Declarations

C++11 implements several features that simplify declarations, particularly for situations arising when templates are used.

auto

C++11 strips the keyword auto of its former meaning as a storage class specifier (Chapter 9, “Memory Models and Namespaces”) and puts it to use (Chapter 3) to implement automatic type deduction, provided that an explicit initializer is given. The compiler sets the type of the variable to the type of the initialization value:

auto maton = 112;  // maton is type intauto pt = &maton;  // pt is type int *double fm(double, int);auto pf = fm;      // pf is type double (*)(double,int)

The auto keyword can simplify template declarations too. For example, if il is an object of type std::initializer_list<double>

Get C++ Primer Plus 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.