C++11 Language Extensions - General Features
original link: http://isocpp.org/wiki/faq/cpp11-language
Contents of this section:
Contents of this section:
Auto
auto
Consider
and the type of x will be the type of the value computed from “expression”.
The use of auto to deduce the type of a variable from its initializer is obviously most useful when that type is either hard to know exactly or hard to write. Consider:
In C++98, we’d have to write
When the type of a variable depends critically on template argument it can be really hard to write code without auto. For example:
The type of tmp should be what you get from multiplying a T by a U, but exactly what that is can be hard for the human reader to figure out, though of course the compiler knows once it has figured out what particular T and U it is dealing with.
The auto feature has the distinction to be the earliest to be suggested and implemented: Stroustrup had it working in his Cfront implementation in early 1984, but was forced to take it out because of C compatibility problems. Those compatibility problems disappeared when C++98 and C99 accepted the removal of “implicit int”; that is, both languages now require every variable and function to be defined with an explicit type. The old meaning of auto (namely “this is a local variable”) is now illegal. Several committee members trawled through millions of lines of code finding only a handful of uses – and most of those were in test suites or appeared to be bugs.
Being primarily a facility to simplify notation in code, auto does not affect the standard library specification.
See also:
Consider
- auto x = 7;
- auto x = expression;
and the type of x will be the type of the value computed from “expression”.
The use of auto to deduce the type of a variable from its initializer is obviously most useful when that type is either hard to know exactly or hard to write. Consider:
- template<class T> void printall(const vector<T>& v)
- {
- for (auto p = v.begin(); p!=v.end(); ++p)
- cout << *p << "\n";
- }
In C++98, we’d have to write
- template<class T> void printall(const vector<T>& v)
- {
- for (typename vector<T>::const_iterator p = v.begin(); p!=v.end(); ++p)
- cout << *p << "\n";
- }
When the type of a variable depends critically on template argument it can be really hard to write code without auto. For example:
- template<class T, class U> void multiply(const vector<T>& vt, const vector<U>& vu)
- {
- // ...
- auto tmp = vt[i]*vu[i];
- // ...
- }
The type of tmp should be what you get from multiplying a T by a U, but exactly what that is can be hard for the human reader to figure out, though of course the compiler knows once it has figured out what particular T and U it is dealing with.
The auto feature has the distinction to be the earliest to be suggested and implemented: Stroustrup had it working in his Cfront implementation in early 1984, but was forced to take it out because of C compatibility problems. Those compatibility problems disappeared when C++98 and C99 accepted the removal of “implicit int”; that is, both languages now require every variable and function to be defined with an explicit type. The old meaning of auto (namely “this is a local variable”) is now illegal. Several committee members trawled through millions of lines of code finding only a handful of uses – and most of those were in test suites or appeared to be bugs.
Being primarily a facility to simplify notation in code, auto does not affect the standard library specification.
See also:
- the C++ draft section 7.1.6.2, 7.1.6.4, 8.3.5 (for return types)
- [N1984=06-0054] Jaakko Jarvi, Bjarne Stroustrup, and Gabriel Dos Reis: Deducing the type of variable from its initializer expression (revision 4).
- Herb Sutter: GotW #92: Auto Variables, Part 1
- Herb Sutter: GotW #93: Auto Variables, Part 2
- Herb Sutter: GotW #94: AAA Style (Almost Always Auto)