19

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?

Columbo
  • 60,038
  • 8
  • 155
  • 203
curiousguy12
  • 1,741
  • 1
  • 10
  • 15

1 Answers1

15

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?)

Columbo
  • 60,038
  • 8
  • 155
  • 203