Categories

Jan 4 in C++
Q: Alternate type deduction on declaration- C++ 14 Feature

1 Answer

Jan 4
In C++11, two methods of type deduction were added. auto was a way to create a variable of the appropriate type, based on a given expression. decltype was a way to compute the type of a given expression. However, decltype and auto deduce types in different ways. In particular, auto always deduces a non-reference type, as though by using std::decay, while auto&& always deduces a reference type. However, decltype can be prodded into deducing a reference or non-reference type, based on the value category of the expression and the nature of the expression it is deducing:[3]

int   i;

int&& f();

auto          x3a = i;     // decltype(x3a) is int

decltype(i)   x3d = i;     // decltype(x3d) is int

auto          x4a = (i);   // decltype(x4a) is int

decltype((i)) x4d = (i);   // decltype(x4d) is int&

auto          x5a = f();   // decltype(x5a) is int

decltype(f()) x5d = f();   // decltype(x5d) is int&&

C++14 adds the decltype(auto) syntax. This allows auto declarations to use the decltype rules on the given expression.

The decltype(auto) syntax can also be used with return type deduction, by using decltype(auto) syntax instead of auto for the function's return type deduction
Click here to read more about C++
Click here to read more about Insurance

Related questions

Madanswer
Jan 4 in C++
Jan 4 in C++
Jul 28, 2019 in R Language
...