Search code examples
c++referencelvaluervalue

Taking reference of rvalue


according to the open standard (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2027.html)

This code should give an error:

A&  a_ref3 = A();  // Error!
A&& a_ref4 = A();  // Ok

However, I am able to do the first line just fine. And the second line actually errors for me. I am compiling with Visual C++14 in Visual Studio.

class foo {

};

int main()
{

    foo x;
    foo() = x; // Valid

    foo& fr = foo(); // Valid

    string&& foo = "hey"; // Valid r reference

    foo&& frr = foo(); // Error!

    return 0;
}

So I guess my question is that is foo() an lvalue? And if so, why does Stroustrup say that you cannot assign a reference type to it?


Solution

  • foo& fr = foo(); works in VC++ because the compiler allows it, clang and gcc both reject this syntax.

    foo&& frr = foo(); on the other hand is an error because you've redeclared foo and are trying to call operator() on a std::string, which isn't defined.