decltype is a way to specify a type: you give it an expression, and decltype returns a type that matches the type of the expression. In particular, decltype(e) is the following type:
If e is the name of the variable, that is, "id-expression", then the resulting type is the type of the variable.
Otherwise, if e evaluates a value l of type T , then the resulting type is T & , and if e evaluates to rvalue of type T , then the resulting type is T
Combining these rules with standard folding rules allows you to understand decltype(e) && , which is always a βsuitableβ link. (C ++ 14 also adds decltype(auto) to give you the auto output type in combination with decltype value category semantics.)
Examples:
int foo(); int n = 10; decltype(n) a = 20;
One could emphasize the difference between auto and decltype : auto works on types, and decltype works on expressions.
You should not see or use decltype in day-to-day programming. This is most useful in general (template) library code, where the specified expression is unknown and depends on the parameter. (In contrast, auto can be used generously all over the place.) In short, if you are new to programming, you probably won't need to use decltype for some time.
Kerrek SB
source share