成员模板,ISO C ++标准声明?

时间:2010-08-17 05:29:05

标签: c++ templates

可以解释一下吗?

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

请用程序解释..... 该声明来自ISO C ++标准14.5.2部分,第8点

1 个答案:

答案 0 :(得分:3)

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 
}

编辑:第一次评论后

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*()
}

上面的代码显示了涉及模板/非模板时的部分排序和重载解析。