tags:

views:

53

answers:

1

can any one explain this?

   "Overload resolution and partial ordering are used to select the best
    conversion function among multiple template conversion functions and
    or non-template conversion functions."

Please explain with a program..... the statement is from ISO C++ Standard 14.5.2 section ,point 8

+4  A: 
struct S{
   template<class T> operator T(){return T();}
   operator int(){return 0;}
};

int main(){
   S s;
   int xi = s;   // both template and non template are viable. Overload res chooses non tmpl
   char xc = s;  // both template and non template are viable. Overload res chooses tmpl 
}

Edit: After first comment

struct B{
   operator int(){return 0;}
};

struct S : B{
   template<class T> operator T(){return T();}
   operator int(){return 0;}
   template<class T> operator T*(){return T();}
};

int main(){
   S s;
   int xi = s;  // Overload reslution between operator T and operator int
   char xc = s; // Overload resolution between operator T and operator int
   int *pi = s; // Partial ordering involved between operator T() and operator T*()
}

The code above shows partial ordering and overload resolution when template/non-template both are involved.

Chubsdad
Yes, the second program shifted operator int to base class. That code snippet did not involve any overloading....Overloading is not between base/derived class scopes.
Chubsdad
Simply speaking it means that, both operator T() and operator T*() are viable functions for converting 's' to 'int *'. However based on C++ rules, operator T*() is considered to be more specialized than operator T. I like the reference to Section 12.2.2/3 of "C++ Templates" by David-Vandevoorde.Again taking a very simplistic view, this means that the conversions that operator T*() can be applied to is a subset of the conversions that operator T() can be applied to, and hence operator T*() is considered to be more specialized than operator T().
Chubsdad
Thank you...for your clear answer
BE Student