It seems that auto
was a fairly significant feature to be added in C++11 that seems to follow a lot of the newer languages. As with a language like Python, I have not seen any explicit variable declaration (I am not sure if it is possible using Python standards).
Is there a drawback to using auto
to declare variables instead of explicitly declaring them?
You've only asked about drawbacks, so I'm highlighting some of those. When used well,
auto
has several advantages as well. The drawbacks result from ease of abuse, and from increased potential for code to behave in unintended ways.The main drawback is that, by using
auto
, you don't necessarily know the type of object being created. There are also occasions where the programmer might expect the compiler to deduce one type, but the compiler adamantly deduces another.Given a declaration like
you don't necessarily have knowledge of what type
result
is. It might be anint
. It might be a pointer. It might be something else. All of those support different operations. You can also dramatically change the code by a minor change likebecause, depending on what overloads exist for
CallSomeFunction()
the type of result might be completely different - and subsequent code may therefore behave completely differently than intended. You might suddenly trigger error messages in later code(e.g. subsequently trying to dereference anint
, trying to change something which is nowconst
). The more sinister change is where your change sails past the compiler, but subsequent code behaves in different and unknown - possibly buggy - ways.Not having explicit knowledge of the type of some variables therefore makes it harder to rigorously justify a claim that the code works as intended. This means more effort to justify claims of "fit for purpose" in high-criticality (e.g. safety-critical or mission-critical) domains.
The other, more common drawback, is the temptation for a programmer to use
auto
as a blunt instrument to force code to compile, rather than thinking about what the code is doing, and working to get it right.This isn't a drawback of
auto
in a principled way exactly, but in practical terms it seems to be an issue for some. Basically, some people either: a) treatauto
as a savior for types and shut their brain off when using it, or b) forget thatauto
always deduces to value types. This causes people to do things like this:Oops, we just deep copied some object. It's often either a bug or a performance fail. Then, you can swing the other way too:
Now you get a dangling reference. These problems aren't caused by
auto
at all, so I don't consider them legitimate arguments against it. But it does seem likeauto
makes these issue more common (from my personal experience), for the reasons I listed at the beginning.I think given time people will adjust, and understand the division of labor:
auto
deduces the underlying type, but you still want to think about reference-ness and const-ness. But it's taking a bit of time.It makes your code a little harder, or tedious, to read. Imagine something like that:
Now, to figure out the type of output, you'd have to track down signature of
doSomethingWithData
function.Keyword
auto
simply deduce the type from the return value. Therefore, it is not equivalent with a Python object, e.g.Since
auto
is deduced during compilation, it won't have any drawback at runtime whatsoever.One of the drawbacks is that sometimes you can't declare
const_iterator
withauto
. You will get ordinary (non const) iterator in this example of code taken from this question:One reason that I can think of is that you lose the opportunity to coerce the class that is returned. If your function or method returned a long 64 bit, and you only wanted a 32 unsigned int, then you lose the opportunity to control that.