c++ - 标准最新草案第 §8.5.3/5 段中的要点 (5.2.2.1) 中的 "user-defined conversions are not considered"是什么意思?

标签 c++ reference language-lawyer c++17

§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/

相关文章:

c++ - 为什么新 C++ 标准使用 "ISO/IEC 14882:2015"?

c++ - 为什么这个静态 const int 成员变量似乎可以在数组定义中公开访问?

c++ - 在函数参数列表中向前声明的类型如何在函数范围外可见?

c++ - 显示属于树的深度路径的二叉搜索树的节点

c++ - Jsoncpp问题

c# - 有趣的 "params of ref"功能,有什么解决方法吗?

c++ - 多线程应用程序c++中对象的引用计数

c++ - 如何获取指向引用成员的指针?

c++ - "if constexpr(something false)"是否总是省略模板实例化

c++ - C/C++ - 为 PUT 请求实现 http 协议(protocol)