Re: What does the function parameter "..." mean?
On Mar 1, 7:05 am, Ivan <linyongt...@gmail.com> wrote:
Recently, I read a simple code in the book C++ Templates.
Bellow is the code:
template<typename T>
class IsClassT {
private:
typedef char One;
typedef struct { char a[2]; } Two;
template<typename C> static One test(int C::*);
template<typename C> static Two test(...);
//Here, what does ... mean?
public:
enum { Yes = sizeof(IsClassT<T>::test<T>(0)) == 1 };
enum { No = !Yes };
};
In code, I don't know clear what ... means? But I guess it as:
1) ... means variable argument. But as I known, variable argument is a
placeholder of arguments which must be placed after the last named
argument in C language. Is this ture in C++? if so, ... doesn't means
variable argument.
It means that the function takes 0 or more arguments in addition
to the ones specified. Both in C and in C++, and int f(...) is
perfectly legal in C. The reference to the "last named
argument" is for <cstdarg>/<stdarg.h>; in order to access the
arguments in a standard manner, you need at least one named
argument.
2) ... means wildcard argument. In function overload, if any
function test can't be selected in overload resolution, then
the one with ... as paramenter will be selected.
Sort of. Since ... matches 0 or more arguments of any type, it
will match whatever is passed to it. It would also match if
the function were called without an argument, or with two or
more arguments, but those cases aren't relevant here. The
second important point is that in overload resolution, matching
an argument to ... is considered the poorest match, so matching
any other argument type is better.
In this particular case, the trick being used is that the
inability to instantiate a declaration of a function template
during overload resolution is not considered an error; it only
means that that function template will not be considered during
overload resoltion. In the above, if C is a class type, the
compiler can instantiation both of the template functions, and
the conversion of 0 to a null pointer to member is preferred
over having it match ... (even though that would imply no
conversion). If C is not a class type, trying to instantiation
a function whose parameter type is C::* will fail, so the only
available function is the one matching ... (Of course, in the
above, neither function is ever called, so they don't need
implementations.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34