Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading