c++ - 默认编译器生成引用运算符(在 C++ 中)?

标签 c++ compiler-construction reference operator-overloading default

所有作者都写道,默认情况下,编译器会生成

1. the default constructor
2. copy constructor
3. assignment operator
4. destructor

有一天,我的一位 friend 受邀参加面试,他被告知默认情况下,编译器仍然生成一个函数引用运算符(运算符 &())。事实上是这样吗?

最佳答案

不,这不是真的。如果没有 operator& 存在,则使用核心语言中的运算符。

N3485 13.3.1.2 [over.match.oper]/1-2(强调我的):

If no operand of an operator in an expression has a type that is a class or an enumeration, the operator is assumed to be a built-in operator and interpreted according to Clause 5. [ Note: Because ., .*, and :: cannot be overloaded, these operators are always built-in operators interpreted according to Clause 5. ?: cannot be overloaded, but the rules in this subclause are used to determine the conversions to be applied to the second and third operands when they have class or enumeration type (5.16). —end note ]

If either operand has a type that is a class or an enumeration, a user-defined operator function might be declared that implements this operator or a user-defined conversion can be necessary to convert the operand to a type that is appropriate for a built-in operator. In this case, overload resolution is used to determine which operator function or built-in operator is to be invoked to implement the operator.

您还可以看到这一点,因为为您隐式声明的函数被列为“特殊成员函数(第 12 条),它没有引用 operator&

关于c++ - 默认编译器生成引用运算符(在 C++ 中)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18201645/

相关文章:

依赖于编译器特定代理的 C++11 库功能

Excel VBA 编译错误

java - 将 InputArrayOfArrays 从 java 类传递到 JNI

c++ - 需要关于实现限时试验的建议

c++ - A a; 之间有什么区别吗?和 A = A()?

java - 编译并升级到JDK 1.7

c++ - Dev-C++ - [构建错误] [main.o] 错误 1

c++ - 在 C++ 中捕获错误的数组引用

c++ - 为什么 "universal references"具有与右值引用相同的语法?

c++ - 为了学习 C++,我需要一种在 Mac 英特尔上编译 C++ 代码的简单方法。我