C++ offers two mechanisms for deducting types from an expression: auto and decltype(). auto is used to deduce a type from its initializer, while decltype() is used to deduce a type for more complex cases. This recipe will show examples of how to use both.
Automatic type deduction and decltype
How to do it...
It might be handy (and it actually is) to avoid explicitly specifying the type of variable that will be used, especially when it is particularly long and used very locally:
- Let's start with a typical example:
std::map<int, std::string> payslips;
// ...
for (std::map<int,
std::string>::const_iterator iter = payslips.begin();
iter !=payslips.end(); ++iter)
{
// ...
}
- Now, let&apos...