在C++中,右值可以用作赋值运算符的左手操作数吗?

问题描述 投票:0回答:0
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 上成功编译。

c++ compiler-construction variable-assignment rvalue
© www.soinside.com 2019 - 2024. All rights reserved.