struct Test{
Test(int x):value(x){}
Test& operator=(const Test& that) {
value = that.value; // #1
return *this;
}
int value{0};
};
int main(){
Test(12) = Test(1024); // call Test::operator=(const Test&)
return 0;
}
在#1 语句中,一个内置右值被分配给另一个内置右值。 但是在cppreference值分类页面,可以看到这句话:
右值不能用作内置赋值或复合赋值运算符的左侧操作数。
so,为什么我的代码可以编译成功?是不是编译器(gcc/msvc)不符合C++标准?
我的代码可以在 gcc 和 msvc 上成功编译。