One problem is that when you write a template function, it’s not always possible in C++98 to know what type to use in a declaration. Consider this partial example:
template
void ft(T1 x, T2 y)
{
...
?type? xpy = x + y;
...
}
What should the type for xpy be? We don’t know in advance how ft() might be used. The proper type might be T1 or T2 or some other type altogether. For example, T1 could be double and T2 could be int, in which case the type of the sum is double. Or T1 could be short and T2 could be int, in which case the type of the sum is int. Or suppose T1 is short and T2 is char. Then addition invokes automatic integer promotions, and the resultant type is int. Also the + operator can be overloaded for structures and classes, complicating the options further. Therefore, in C++98 there is no obvious choice for the type of xpy.
The decltype Keyword (C++11)
The C++11 solution is a new keyword: decltype. It can be used in this way:
int x;
decltype(x) y; // make y the same type as x
The argument to decltype can be an expression, so in the ft() example, we could use this code:
decltype(x + y) xpy; // make xpy the same type as x + y
xpy = x + y;
Alternatively, we could combine these two statements into an initialization:
decltype(x + y) xpy = x + y;
So we can fix the ft() template this way:
template
void ft(T1 x, T2 y)
{
...
decltype(x + y) xpy = x + y;
...
}
The decltype facility is a bit more complex than it might appear from these examples. The compiler has to go through a checklist to decide on the type. Suppose we have the following:
decltype(
Here’s a somewhat simplified version of the list.
Stage 1: If
double x = 5.5;
double y = 7.9;
double ℞ = x;
const double * pd;
decltype(x) w; // w is type double
decltype(rx) u = y; // u is type double &
decltype(pd) v; // v is type const double *
Stage 2: If
long indeed(int);
decltype (indeed(3)) m; // m is type int
Note
The call
Stage 3: If
double xx = 4.4;
decltype ((xx)) r2 = xx; // r2 is double &
decltype(xx) w = xx; // w is double (Stage 1 match)
Incidentally, parentheses don’t change the value or lvaluedness of an expression. For example, the following two statements have the same effect:
xx = 98.6;
(xx) = 98.6; // () don't affect use of xx
Stage 4: If none of the preceding special cases apply, var is of the same type as
int j = 3;
int &k = j
int &n = j;
decltype(j+6) i1; // i1 type int
decltype(100L) i2; // i2 type long
decltype(k+n) i3; // i3 type int;
Note that although k and n are references, the expression k+n is not a reference; it’s just the sum of two ints, hence an int.
If you need more than one declaration, you can use typedef with decltype:
template
void ft(T1 x, T2 y)
{
...
typedef decltype(x + y) xytype;
xytype xpy = x + y;
xytype arr[10];
xytype & rxy = arr[2]; // rxy a reference
...
}
Alternative Function Syntax (C++11 Trailing Return Type)
The decltype mechanism by itself leaves another related problem unsolved. Consider this incomplete template function:
template
?type? gt(T1 x, T2 y)
{
...
return x + y;
}
Again, we don’t know in advance what type results from adding x and y. It might seem that we could use decltype(x + y) for the return type. Unfortunately, at that point in the code, the parameters x and y have not yet been declared, so they are not in scope (visible and usable to the compiler). The decltype specifier has to come
double h(int x, float y);
can be written with this alternative syntax:
auto h(int x, float y) -> double;
This moves the return type to after the parameter declarations. The combination -> double is called a
auto h(int x, float y) -> double
{/* function body */};