7

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
  • 2
    You have not stated why you feel uncomfortable using auto. So it's impossible for us to determine whether your reasons are justified or not. Commented May 19, 2014 at 0:42
  • I'm not sure there's sufficient difference between languages to keep this and this (and various others) apart.
    – Telastyn
    Commented May 19, 2014 at 0:47
  • 3
    If you have every written a template, you've written code that works with an object without knowing its type.
    – user53141
    Commented May 19, 2014 at 0:56
  • 1
    @vector - none of those (incorrect) things impact if you should use auto/var or not.
    – Telastyn
    Commented May 19, 2014 at 2:00
  • 1
    /strongly/statically/g, the former means absolutely nothing.
    – user7043
    Commented May 19, 2014 at 8:34

2 Answers 2

23

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
  • Excellent guidelines - accepted.
    – Vector
    Commented May 19, 2014 at 4:58
  • 2
    Just nitpicking: in this case, one might as well use the range-based for-loop instead for better clarity (unless you really need the iterator, and not the value). Perhaps another example would illustrate the point better ? (still +1 for me)
    – ereOn
    Commented May 19, 2014 at 7:43
  • 1
    (Should even be a range-based for loop for even more goodness.)
    – Mat
    Commented May 19, 2014 at 8:04
  • 1
    @ereOn: Using iterators announces that the body of the loop will only deal with a single element at a time, whereas indexing you may use multiple by offsetting the index. Each has its advantages. Additionally, while the performance of iterator vs indexing for vectors is pretty much the same, that is not necessarily true for other containers, where one can be significantly faster than the other. stackoverflow.com/questions/131241/… Commented May 19, 2014 at 13:14
5

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
  • 2
    You are still getting strong types even if they aren't explicitly specified - I never said auto is not strongly typed, just that you don't explicitey see the types.
    – Vector
    Commented May 19, 2014 at 3:42
  • @BartvanIngenSchenau - no right to complain that your type name spans three lines and makes the code unreadable Curious to know where you found any mention of a complaint in my question... the creator of C++ himself (cited in the question) advocates using auto and gives some guidelines on how to use it - I'm not going to "complain". I asked for clarification on using auto because most of us like to know what our code is doing and auto feels a bit strange and hacked - meaning that I'm uncertain about its appropriate use, which the accepted answer elaborates on.
    – Vector
    Commented May 19, 2014 at 17:26
  • 1
    Why is it strange to let the computer handle what the computer knows? auto is very useful for situations where the compiler will only accept one value. Why should I waste time figuring out what it knows? It doesn't feel like a "hack" at all to have not have to tell the compiler what it already knows. "This variable should just have whatever type this function returns" seems a lot more reasonable than "I declare this is an int but if the function returns something else, tell me that I'm wrong, or maybe implicitly convert in a way that will trip me up"
    – user53141
    Commented May 19, 2014 at 17:58
  • 2
    @StevenBurnap Why is it strange... Not sure "strange" is the right word, but the point here is not what the computer knows, but what the programmer knows: Imagine you 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.
    – Vector
    Commented May 20, 2014 at 11:01

Not the answer you're looking for? Browse other questions tagged or ask your own question.