开发者

Member templates ,Statemement From ISO C++ Standard?

开发者 https://www.devze.com 2023-01-11 17:00 出处:网络
can any one explain this? \"Overload resolution and partial ordering are used to select the best conversion function among multiple template conversion functions and

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 wi开发者_开发问答th a program..... the statement is from ISO C++ Standard 14.5.2 section ,point 8


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号