2

Like printf?

But I remember that C++ does name mangling with function name and parameter types,

this can deduce that C++ doesn't support variable length parameters...

I just want to make sure, is that the case?

UPDATE

the discussion should exclude those included by extern "C"

Mark B
  • 95,107
  • 10
  • 109
  • 188
lexer
  • 1,027
  • 3
  • 14
  • 18
  • 1
    Name mangling is based on the function signature. A varargs function still *has* a signature, with a parameter list that ends in `...` (and which doesn't involve the types of the arguments the caller provides as varargs). A correct function declaration must be in scope to call it. So the caller and the callee agree on the function signature and hence the mangled name. If you want to know what ASCII characters are used to represent the `...` in the mangled name in your implementation, check the symbols in your executable. – Steve Jessop Sep 20 '11 at 15:19
  • Although C++ uses varargs for variable length parameters, you **don't** have to mark such functions as `extern "C"`. – Max Lybbert Sep 20 '11 at 15:25

2 Answers2

6

Yes. C++ inherits this from C. You can write:

void f(...);

This function can take any number of arguments of any types. But that is not very C++-style. Programmers usually avoid such coding.

However, there is an exception: in template programming, SFINAE makes use of this a lot. For example, see this (taken from here):

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);
};

This uses test(...) which is a variadic function.

Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • BTW, why do you still use `struct` in C++ where class is available? – lexer Sep 20 '11 at 15:27
  • @lexer: That is another question, not related to this topic. Anyway, in C++, you can define a *class* using both keywords : `struct` and `class`. Note that in the code above, it's called, class template, even though it has used `struct` keyword. Search other topics at stackoverflow, to know the difference between them. – Nawaz Sep 20 '11 at 15:31
  • @lexer: For example, you can read this : [struct vs class in C++](http://stackoverflow.com/questions/2750270/c-c-struct-vs-class) – Nawaz Sep 20 '11 at 15:32
4

Yes, C++ supports the ellipsis from C, but I strongly advice against using them, since they are in no way type safe.

If you have access to a good C++11 capable compiler with variadic template support, then you should use that instead. If you don't have that, have a look at how boost::format solves those things.

PlasmaHH
  • 15,673
  • 5
  • 44
  • 57