Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the purpose of function with only unspecified number of parameters?

In other words when function declared like this with 'T' being some type-alias:

T (...)

will be ever useful?

If you don't know such declaration specifies a function with unknown number of parameters. It's allowed by the C++ standard but it doesn't provide us with a standard way of accessing passed arguments. There is <cstdarg> library but it require named parameter before the ellipsis in order to work. It look like this (with another type-alias named 'T1'):

T (T1, ...)

Normally T1 is of type int and sepcifies the number of variadic arguments.

However the fact that the ellipsis can be the only function parameter means that such construct have some purpose and I'm curios what is it?

An actual example of such function will look like this:

void func(...)
{
}
like image 456
AnArrayOfFunctions Avatar asked Apr 18 '15 13:04

AnArrayOfFunctions


1 Answers

One example is the metaprogramming trick to take advantage of the fact that ... is always a viable overload but is the least preferred. For example, this type trait checks if a particular member (foo) exists:

template <typename T>
struct has_foo {
    template <typename U>
    static std::true_type test( decltype(U::foo)* );

    template <typename U>
    static std::false_type test( ... );

    using type = decltype(test<T>(0));
};
like image 179
Barry Avatar answered Nov 06 '22 06:11

Barry