C++ vararg函数指针

Tho*_*ing 1 c++ variadic-functions

以下是否导致明确定义的行为?也就是说,如果你将非vararg函数f作为vararg函数g转换并使用f期望的参数调用g,那么行为是否与使用这些参数调用f的行为相匹配?

class Base {};

class Derived1 : public Base {
public:
    int getInt1() {return 1;}
};

class Derived2 : public Base {
public:
    int getInt2() {return 2;}
};

typedef int (*vfunc)(...);

int foo (vfunc f) {
    Derived1 d1;
    Derived2 d2;
    return f(&d1, &d2);
}

int bar (Derived1 * p1, Derived2 * p2) {
    return p1->getInt1() + p2->getInt2();
}

int main (int argc, char ** argv) {
    return foo((vfunc)bar); // Is this program guaranteed to return 3?
}
Run Code Online (Sandbox Code Playgroud)

UPDATE

有没有什么方法可以让程序定义明确,即使使用专有关键字?比如做一些像__cdecl这里提到的东西:

http://msdn.microsoft.com/en-us/library/984x0h58%28v=vs.80%29.aspx

我的最终目标是让一个matcher函数尝试匹配X指针列表.matcher函数接受一个谓词(不一定是函数......可能是一个列表),并接受一个函数,它将匹配的结果传递给.传递给它的回调函数采用与谓词匹配相同的参数类型和arity.

Jam*_*lis 6

不,根据C++ 11 5.2.11/6(reinterpret_cast),行为是未定义的:

通过指向函数类型的指针调用函数的效果是未定义的,该函数类型与函数定义中使用的类型不同.

类型barint(Derived1*, Derived2*).函数的类型f(通过其进行调用的表达式)是指向的函数的类型int(...).两者不一样,因此行为未定义.