-
Book Overview & Buying
-
Table Of Contents
C++ Memory Management
By :
The type system of C++ is powerful and nuanced, offering (among other things) a set of type deduction facilities. The best-known type deduction tool is probably auto, used to infer the type of an expression from the type of its initializer:
const int n = f(); auto m = n; // m is of type int auto & r = m; // r is of type int& const auto & cr0 = m; // cr0 is of type const int& auto & cr1 = n; // cr1 is of type const int&
As you might notice from the preceding example, by default, auto makes copies (see the declaration of variable m ), but you can qualify auto with &, &&, const, and so on if needed.
Sometimes, you want to deduce the type of an expression with more precision, keeping the various qualifiers that accompany it. That might be useful when inferring the type of an arithmetic expression, the type of a lambda, the return type of a complicated generic function, and so on. For this, you have the decltype operator...