gpt4 book ai didi

c++ - 重载转换函数模板

转载 作者:IT老高 更新时间:2023-10-28 22:41:43 26 4
gpt4 key购买 nike

考虑以下几点:

struct X {
template <class T> operator T(); // #1
template <class T> operator T&(); // #2
};

int a = X{}; // error: ambiguous
int& b = X{}; // calls #2
int const& c = X{}; // calls #2

b 的情况很简单,#2是唯一可行的候选人。指示#2 的规则是什么?优先于 #1用于初始化 int const& , 但两者对于 int 的初始化不明确?

最佳答案

在决定如何初始化给定初始化器的引用时,首先尝试直接绑定(bind)。 [dcl.init.ref]/(5.1.2) :

If the reference is an lvalue reference and the initializer expression […] has a class type (i.e., T2 is a class type), where T1 is not reference-related to T2, and can be converted to an lvalue of type “cv3 T3”, where “cv1 T1” is reference-compatible with “cv3 T3” (this conversion is selected by enumerating the applicable conversion functions (13.3.1.6) and choosing the best one through overload resolution (13.3)), then the reference is bound […] to the lvalue result of the conversion […].

控制此过程的候选者选择的措辞(13.3.1.6,如上所述)不包括第一个转换函数:

The conversion functions of S and its base classes are considered. Those non-explicit conversion functions that are not hidden within S and yield type “lvalue reference to cv2 T2(when initializing an lvalue reference or an rvalue reference to function) […], where “cv1 T” is reference-compatible (8.6.3) with “cv2 T2”, are candidate functions. For direct-initialization, […].

显然,这种排除是特定于引用初始化语义的,所以第一种情况仍然不明确。

关于c++ - 重载转换函数模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39277869/

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