Dan Nissenbaum
Dan Nissenbaum

Reputation: 13908

Allowed to bind an rvalue to a non-const lvalue reference?

In studying rvalues and rvalue references, I've been pointed to the excellent posting https://stackoverflow.com/a/11540204/368896, in which appears the following table:

            lvalue   const lvalue   rvalue   const rvalue
---------------------------------------------------------              
X&          yes
const X&    yes      yes            yes      yes
X&&                                 yes
const X&&                           yes      yes

Note that the table indicates that an rvalue cannot bind to a non-const lvalue reference.

However, in VS2010 I seem to be able to do so:

class A
{};

int main()
{
    A & a = A(); // Binding an rvalue to a non-const lvalue reference?
}

Where is my misunderstanding?

Upvotes: 2

Views: 802

Answers (1)

Kerrek SB
Kerrek SB

Reputation: 477060

This is a compiler "extension" (or "bug", depending on your perspective) of the Microsoft compiler. C++ only allows non-const binding of an lvalue to a non-const lvalue reference.

Upvotes: 3

Related Questions