C++是否支持可变数量的函数参数?

lex*_*xer 2 c++

喜欢printf

但我记得C++确实用函数名和参数类型命名,

这可以推断出C++不支持可变长度参数......

我只是想确定,是这样的吗?

UPDATE

讨论应排除包括在内的那些 extern "C"

Naw*_*waz 6

是.C++从C继承了这个.你可以写:

void f(...);
Run Code Online (Sandbox Code Playgroud)

此函数可以使用任何类型的任意数量的参数.但那不是很C++风格.程序员通常会避免这种编码.

但是,有一个例外:在模板编程中,SFINAE充分利用了这一点.例如,看到这个(取自这里):

template <typename T>
struct has_typedef_type {
    // Variables "yes" and "no" are guaranteed to have different sizes,
    // specifically sizeof(yes) == 1 and sizeof(no) == 2.
    typedef char yes[1];
    typedef char no[2];

    template <typename C>
    static yes& test(typename C::type*);

    template <typename>
    static no& test(...);

    // If the "sizeof" the result of calling test<T>(0) 
    // would be equal to the sizeof(yes), the first overload worked 
    // and T has a nested type named type.
    static const bool value = sizeof(test<T>(0)) == sizeof(yes);
};
Run Code Online (Sandbox Code Playgroud)

这使用的test(...)是可变函数.

  • @lexer name mangling是特定于实现的. (2认同)