加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 为什么这个函数调用没有歧义?

发布时间:2020-12-16 09:57:25 所属栏目:百科 来源:网络整理
导读:我想知道为什么这个函数调用没有歧义: #include iostream#include vectortemplate class Tclass C{public: typedef char c; typedef double d; int fun() {} static c testFun( decltype(C::fun) ) {return c();} static d testFun(...) { return d(); }};i
我想知道为什么这个函数调用没有歧义:

#include <iostream>
#include <vector>

template <class T>
class C
{
public:
    typedef char c;
    typedef double d;

    int fun() {}

    static c testFun( decltype(&C::fun) ) {return c();} 
    static d testFun(...) { return d(); }
};

int main() {
    C<int>::testFun(0); // Why no ambiguity?
}

http://coliru.stacked-crooked.com/a/241ce5ab82b4a018

解决方法

您有标准转换与省略号转换.标准说标准转换是比后者更好的转换顺序. [over.ics.rank] / P2:

a standard conversion sequence (13.3.3.1.1) is a better conversion sequence than a user-defined conversion
sequence or an ellipsis conversion sequence

指向成员的转换是标准转换序列. 0是空指针常量,可以转换为指向成员的指针. [conv.mem] / P1:

A null pointer constant (4.10) can be converted to a pointer to member type; the result is the null member
pointer value of that type and is distinguishable from any pointer to member not created from a null pointer
constant. Such a conversion is called a null member pointer conversion.

因此,首选过载是优选的.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读