Search code examples
c++gccvisual-c++clang

msvc cannot initialize rvalue reference from value


MSVC (std:c++14) is complaining when initializing an rvalue reference using non-static data member of a prvalue (returned by function call).

The code is as follows (or see Compiler Explorer):

struct R {};

struct A {
    R r;
    R getR() { return r; }
};

A getA() { return {}; };

int main() {
    A &&aa = A();            // okay
    A &&aaa = getA();        // okay
    R &&rr = getA().r;       // should be okay, but not
}

Both GCC and Clang seem okay, and MSVC with /std:c++20 is okay as well. Is this a compliance issue of MSVC or am I misunderstanding something?


Solution

  • You just need to kindly ask MSVC to be compliant with permissive minus: /permissive-: https://godbolt.org/z/1rK5dTjnc

    The flag is enabled by default for C++20