Шаблоны членов, Заявление из стандарта ISO C ++? - PullRequest
0 голосов
/ 17 августа 2010

Кто-нибудь может объяснить это?

   "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 ++ Standard 14.5.2, пункт 8

1 Ответ

3 голосов
/ 17 августа 2010
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*()
}

Приведенный выше код показывает частичное упорядочение и разрешение перегрузки, когда оба шаблона / не шаблон.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...