C++11 features the new auto
type declaration, allowing you to work with an object without ever knowing its actual type.
I use only strongly typed languages – C++, Delphi (Object Pascal), Go, etc and I feel uncomfortable (guilty?) using auto
, (or for example the short variable declaration – x:=1
– in Go.) It feels like a hack to me – I use strongly typed languages because they ensure that you know what type you’re using. (With the exception of the abuse of untyped pointers.) Although certainly I appreciate the benefits: Proper type names involving iterators, templates, smart pointers etc can get very lengthy and a bit difficult to determine and declare explicitly, or to parse when reading. Granted, once you know the proper type name, you can “typedef it”, but sometimes getting it right the first time is time consuming and not always so easy.
Or, imagine you that inherit a large, complex project that you need to modify, and every variable possible in that project is declared using auto
– you’re going to have to do a lot more work to understand that code-base than if everything was declared using explicit types.
So, what are some guidelines on when to use auto
and when to sweat it out with full and proper type names? I am currently reading Stroustrup’s A Tour of C++ and he himself there in Chapter 1 advocates using auto
in situations when you know auto
will “get it right”:
We use auto where we don’t have a specific reason to mention the type
explicitly. “Specific reasons” include:• The definition is in a large scope where we want to make the type
clearly visible to readers of our code.• We want to be explicit about a variable’s range or precision (e.g.,
double rather than float).
In the Advice section of Chapter 1 there he also warns:
-Prefer the {}-initializer syntax for declarations with a named type;
-Prefer the = syntax for the initialization in declarations using
auto;
This, because default initialization {}
could result in an incorrect type initialization.
Still, I feel a bit uncomfortable using auto
.
Can anyone perhaps give me some additional guidelines about the use of auto
, and/or debunk my impression that auto
is a hack of sorts and really should be avoided in favor of determining the proper type and then using atypedef
?
18
No, you should not feel uncomfortable using auto
.
Just use it in situations where the type is obvious, or where no one is going to care about it
A classic example (IMO) of where auto is handy:
std::vector<sometype> vec
...
...
//some code
...
...
for(auto iter = vec.begin(); iter != vec.end(); ++iter)
{
//something here
}
Nobody really cares about the details of the iterator variable (std::vector<sometype>::iterator iter
), only that it is an iterator. Explicitly specifying the type just adds visual noise. Often, without auto
, people will create typedefs for commonly used iterators, which can obscure issues and lead to some bizzare error messages if you use the wrong typedef.
4
You are still getting strong types even if they aren’t explicitly specified. You’re going to eventually hit a static type mismatch in most circumstances. The main concern with type inference is accidentally inferring a type too concrete or abstract. In other words, inferring a derived type when you really needed the base type, or vice versa. If it matters what exact type you get, don’t use auto
.
Another concern about type inference is if it might change the public interface of your code. In general, you should make the types of your public interface explicit.
Aside from those circumstances, there’s little reason not to use auto
when it simplifies your code.
4