§8.5.3[dcl.init.ref]/5 要点 (5.2.2.1):
重点是我的
If T1 or T2 is a class type and T1 is not reference-related to T2, user-defined conversions are considered using the rules for copy-initialization of an object of type “cv1 T1” by userdefined conversion (8.5, 13.3.1.4, 13.3.1.5); the program is ill-formed if the corresponding non-reference copy-initialization would be ill-formed. The result of the call to the conversion function, as described for the non-reference copy-initialization, is then used to direct-initialize the reference. For this direct-initialization, user-defined conversions are not considered.
最佳答案
这是标准的附录,旨在解决通过将类与用户定义的转换进行间接引用绑定(bind)而丢弃 cv 限定符的问题。
相关缺陷是 1571 .
给出的例子是这样的:
class A {
public:
operator volatile int &();
};
A a;
const int & ir1a = a.operator volatile int&(); // error!
const int & ir2a = a; // allowed! ir = a.operator volatile int&();
第一个引用初始化无效,因为它从初始化表达式中丢弃了 cv 限定符。然而,根据旧规则,第二个是有效的,因为只考虑转换对象上的 cv 限定符,而不考虑转换运算符返回的引用的 cv 限定符。
我认为您的困惑源于两种可能的转换。考虑用户定义的转换来查看它们是否可以复制初始化 cv1 T1
的非引用,然后该转换的结果用于直接初始化引用,为此用户定义的转换不予考虑。
关于c++ - 标准最新草案第 §8.5.3/5 段中的要点 (5.2.2.1) 中的 "user-defined conversions are not considered"是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34898678/