in C Plus Plus by
Alternate type deduction on declaration- C++ 14 Feature

1 Answer

0 votes
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

Related questions

+1 vote
asked Jan 4, 2020 in C Plus Plus by AdilsonLima
+1 vote
+1 vote
asked Jan 4, 2020 in C Plus Plus by AdilsonLima
+3 votes
asked Jul 28, 2019 in R Language by Aarav2017