gpt4 book ai didi

c++ - 成员模板,来自 ISO C++ 标准的声明?

转载 作者:行者123 更新时间:2023-11-28 01:10:43 27 4
gpt4 key购买 nike

谁能解释一下?

   "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 点

最佳答案

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

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

关于c++ - 成员模板,来自 ISO C++ 标准的声明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3499565/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com