Search code examples
c++c++14language-lawyerrvalue-referencedecltype

Why does this rvalue reference bind to an lvalue?


I do not understand why the following code compiles on GCC 8.0:

decltype(auto) foo(int&& r) {
    return r;
}

In foo, the declaration type of r is int&&, and so the return type of foo is also int&&. But r itself is an lvalue, and an lvalue cannot bind to an rvalue reference.

Am I missing something?


Solution

  • According to [dcl.spec.auto]/5, the return type is deduced as if the return statement's operand was the operand of decltype. And [dcl.type.simple]/(4.2) clearly states that, as the operand is not parenthesized, the type of the entity is the type yielded by decltype, that is, int&&. And indeed, r is an lvalue ([expr.prim.id.unqual]).

    Fortunately, this has been discovered and filed as bug 64892 two years ago. (I wonder why no one could find the time to fix this?)