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"

2
  • 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. Commented Sep 20, 2011 at 15:19
  • Although C++ uses varargs for variable length parameters, you don't have to mark such functions as extern "C". Commented Sep 20, 2011 at 15:25

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

BTW, why do you still use struct in C++ where class is available?
@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.
@lexer: For example, you can read this : struct vs class in C++
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.