tags:

views:

117

answers:

3
   The presence of unused ellipsis and default arguments has no effect
   on the partial ordering of function templates. 

   [Example:

    template<class    T>   void   f(T);                 // #1
    template<class    T>   void   f(T*, int=1);         // #2
    template<class    T>   void   g(T);                 // #3
    template<class    T>   void   g(T*, ...);           // #4

    int main() {
             int* ip;
             f(ip);        // calls #2
             g(ip);        // calls #4
     }

I am unable to understand the above point and example. Can any one explain with another example..for this same statement ..

This is the statement from ISO C++ Standard. section :14.5.5.2 Partial ordering of function templates ...Point 6 Can any one explain this? Otherwise, explain with another example

+5  A: 

It means, that the partial ordering works like you had an overload set like this:

                                                    // #1
template<class    T>   void   f(T);
                                                    // #2a
template<class    T>   void   f(T*);
                                                    // #2b
template<class    T>   void   f(T*, int);
                                                    // #3
template<class    T>   void   g(T);
                                                    // #4a
template<class    T>   void   g(T*);
                                                    // #4b
template<class    T, class U>   void   g(T*, U);
                                                    // #4c
template<class    T, class U, class V>   void   g(T*, U, V);

//(Imagine lots more #4 overloads)

int main() {
         int* ip;
         //calls function #2a, not #1, because T* is a better match than T (#1).
         f(ip); 

         //calls function #4a, not #3, because T* is a better match than T (#3)
         g(ip); 
 }

It would be reasonable to come up with overload ordering rules that would treat #3 as a better match than #4, because of the ellipsis in #4. But that's not how C++ works, and this rule is explaining so. A similar rule applies to default arguments - #2 is called, not #1, and this is illustrating that as well.

Doug
+4  A: 

The "partial ordering" refers to which specialisation of a template is chosen to match the arguments given. The most specialised version that can match the arguments is chosen; in this case, the specialisations for pointer types are more specialised than those for completely generic types, so they are chosen to match the pointer arguments.

The statement says that this decision won't be affected if the function has optional arguments that aren't used when the function is called. #2 and #4 will still be chosen to match arguments of pointer type, as they would if they didn't have optional arguments.

Mike Seymour
+2  A: 
template<class    T>   void   f(T);                 // #1
template<class    T>   void   f(T*, int=1);         // #2
template<class    T>   void   g(T);                 // #3
template<class    T>   void   g(T*, ...);           // #4

int main() {
         int* ip;
         f(ip);        // calls #2
         g(ip);        // calls #4
 }

f(ip) could call #1 or #2. Regarding the number of arguments (one for #1 or two, with a default one for #2) you could expect that #1 would be called. But you also have to examine the type of the first templated argument.

#1 is for T and #2 is for T*; so with this rule you can expect #2 to be called.

These two rules lead to opposite results. The standard then says that the second rule has a weight superior than the first one, and therefore #2 is called disregarding the first rule which is about default argument and ellipsis.

Cedric H.