在编写转换运算符时,如果我同时提供转换const T&
和T&&
,C++是否总是更喜欢rvalue运算符?在这个小测试中,这似乎是正确的:
#include <algorithm>
#include <stdio.h>
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;
}
Run Code Online (Sandbox Code Playgroud)
打印:
??? ./test
moving!
Run Code Online (Sandbox Code Playgroud)
但也许有人能说出比我能验证的更好的规格吗?
归档时间: |
|
查看次数: |
178 次 |
最近记录: |