作者:武艺最新单曲问月09 | 来源:互联网 | 2022-12-08 12:10
在编写转换运算符时,如果我同时提供转换const T&
和T&&
,C++是否总是更喜欢rvalue运算符?在这个小测试中,这似乎是正确的:
#include
#include
struct holds {
operator int&&() { printf("moving!\n"); return std::move(i); }
operator const int&() const { printf("copying!\n"); return i; }
private:
int i = 0;
};
int main() {
holds h;
int val = h;
}
打印:
??? ./test
moving!
但也许有人能说出比我能验证的更好的规格吗?
1> T.C...:
没有这样的偏好.
在非const对象上调用时,您的示例实际上显示了对const const的非const成员函数的首选项.
这将是模棱两可的.