Why does std::declval add a reference?

The “no temporary is introduced for function returning prvalue of object type in decltype” rule applies only if the function call itself is either the operand of decltype or the right operand of a comma operator that’s the operand of decltype (ยง5.2.2 [expr.call]/p11), which means that given declprval in the OP,

template< typename t >
t declprval() noexcept;

class c { ~ c (); };

int f(c &&);

decltype(f(declprval<c>())) i;  // error: inaccessible destructor

doesn’t compile. More generally, returning T would prevent most non-trivial uses of declval with incomplete types, type with private destructors, and the like:

class D;

int f(D &&);

decltype(f(declprval<D>())) i2;  // doesn't compile. D must be a complete type

and doing so has little benefit since xvalues are pretty much indistinguishable from prvalues except when you use decltype on them, and you don’t usually use decltype directly on the return value of declval – you know the type already.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)