How it works...

The auto keyword is a feature added to C++11 called a placeholder type specifier. In other words, the auto keyword is used to tell the compiler a variable's type will be deduced from its initializer. Unlike other languages that use placeholder types, the auto keyword must still adhere to the strict type system of C++, meaning auto should not be confused with std::any.

For example, the following is possible with std::any:

std::any i = 42;i = "The answer is: 42";

The following is not allowed with auto:

auto i = 42;i = "The answer is: 42";

In the first example, we define std::any, which stores an integer. We then replace the integer inside std::any with a C-style string. With respect to auto, this is not possible as, once the ...

Get Advanced C++ Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.