It will break if your code contains the auto
keyword. In nearly 30 years of C and C++ programming I've never come across any that did. The keyword was introduced in the first C compiler to specify local function variables, but compilers almost immediately became clever enough not to require it, and very little code that uses it will survive today - that's why C++0x chose to recycle it rather than introduce a new keyword which would cause portability problems.
The purpose of the auto keyword in C++0X is to allow the compiler to work out the type of a variable, where this is possible:
vector <int> v;
auto it = v.begin():
the compiler can see that v.begin() must return a vector<int>::iterator
and so can create a variable of that type, saving a lot of keyboarding or typedef creation.
auto
has a lot of uses when it comes down to both generic programming and to save the programmer some typing.
For example, consider this. Would you rather type out:
std::unique_ptr<name::long_type::goes_here> g =
std::make_unique<name::long_type::goes_here>(1,2,3,4)
or:
auto g = std::make_unique<name::long_type::goes_here>(1,2,3,4)
Yes, they're both long but we know the return type and specifying it again is a bit cumbersome to type. This also goes for iterators:
for(auto i = vec.begin(); ...)
vs:
for(std::vector<type>::iterator i = vev.begin(); ...)
Its use in generic programming is also to figure out the return type of a function or if you're doing some generic algorithms where you don't know the type.
For example, consider a very basic example.
template<typename T, typename U>
auto add(T t, U u) -> decltype(t + u) {
return t + u;
}
This allows the compiler to figure out the type of the add operation rather than us trying to figure it out ourselves. Note that in C++14 you can omit the trailing return type. Its uses in generic programming don't stop there either. If we wanted to work with any type of container as a wrapper function for algorithms we could use auto
to help us with it. For example:
template<class Cont>
void my_sort(Cont&& cont) {
using std::begin;
auto first = begin(std::forward<Cont>(cont));
// work with the iterators here
}
In the future (C++14), auto can be used to make polymorphic lambdas as well such as:
[](auto a) { return a + 4; }
Which can be useful as well.
Best Answer
auto
was a keyword that C++ "inherited" from C that had been there nearly forever, but virtually never used because there were only two possible conditions: either it wasn't allowed, or else it was assumed by default.The use of
auto
to mean a deduced type was new with C++11.At the same time,
auto x = initializer
deduces the type ofx
from the type ofinitializer
the same way as template type deduction works for function templates. Consider a function template like this:At point A, a type has been assigned to
T
based on the value passed for the parameter towhatever
. When you doauto x = initializer;
, the same type deduction is used to determine the type forx
from the type ofinitializer
that's used to initialize it.This means that most of the type deduction mechanics a compiler needs to implement
auto
were already present and used for templates on any compiler that even sort of attempted to implement C++98/03. As such, adding support forauto
was apparently fairly easy for essentially all the compiler teams--it was added quite quickly, and there seem to have been few bugs related to it either.When this answer was originally written (in 2011, before the ink was dry on the C++ 11 standard)
auto
was already quite portable. Nowadays, it's thoroughly portable among all the mainstream compilers. The only obvious reasons to avoid it would be if you need to write code that's compatible with a C compiler, or you have a specific need to target some niche compiler that you know doesn't support it (e.g., a few people still write code for MS-DOS using compilers from Borland, Watcom, etc., that haven't seen significant upgrades in decades). If you're using a reasonably current version of any of the mainstream compilers, there's no reason to avoid it at all though.More recent revisions of the standard have added a few new places that
auto
can be used. Starting with C++14, you can useauto
for the type of a parameter to a lambda:This does essentially the same thing as the example above--even though it doesn't explicitly use
template
syntax, this is basically a template that deduces the type of the parameter, and instantiates the template over that type.That was convenient and useful enough that in C++20, the same capability was added for normal functions, not just lambdas.
But, just as before all of this really comes down to using the same basic type deduction mechanism as we've had for function templates since C++98.
auto
allows that to be used in more places, and more conveniently, but the underlying heavy lifting remains the same.