jeudi 27 octobre 2016

What is the rationale behind decltype behavior?

As I understood in C++11 decltype(expression) is used to deduce the exact same type of the given expression. But when the expression is put into parentheses itself, then the deduces type is lvalue reference to the expression type. For example:

int x;
decltype(x) y = x;

is equivalent to int y = x; but,

int x;
decltype((x)) y = x;

is equivalent to int& y = x;.

Respectively

 decltype(auto) f1()
 {
   int x = 0;
   return x; // decltype(x) is int, so f1 returns int
 }

but

 decltype(auto) f2()
 {
   int x = 0;
   return (x); // decltype((x)) is int&, so f2 returns int&
 }

What is the rationale for this behavior to be chose by the standard committee?

Aucun commentaire:

Enregistrer un commentaire