2

Possible Duplicate:
How much is too much with C++0x auto keyword
The new keyword “auto”; When should it be used to declare a variable type?

In C++11, Typing a variable auto instead of, say, int, will let the compiler automatically use the right type, deduced from its initialization context. This comes super handy in situations where the type is obvious but boring to write. Are there pitfalls to be aware of, or reasons why someone would avoid using that?

Community
  • 1
  • 1
qdii
  • 12,505
  • 10
  • 59
  • 116
  • In case the type you expect to get **is** the type of initializer expression, there should be no problems. The problem is, it's not what you sometimes expect. – Andrey Agibalov Dec 08 '11 at 11:14
  • 3
    ¤ One main pitfall is that `auto` allows you to hoist a `private` type out from some class. Typically such a type, e.g. a proxy, is not designed for being used for anything but some special purpose and in a special way. With C++03 one had to use nifty template trick to get access to such types, but with C++11 `auto` it is unfortunately very easy to do inadvertently... Cheers & hth., – Cheers and hth. - Alf Dec 08 '11 at 11:26
  • IMO I love using "auto" for iterators. I don't know if it's safe or not, but I haven't had problems with it. You just have to know what to expect. Although if you're using methods of a class that you initialised with auto, the compiler will probably alert you if the methods don't exist as it's not the right class. – Matej Dec 08 '11 at 11:26
  • 2
    the primary reason for adding auto was not for situations where the type is obvious and boring to write, it was added to help in situations where the type is not-obvious and tricky to write! – Scott Langham Dec 08 '11 at 11:32
  • 1
    Voted to reopen. The dangers do not seem to be discussed in those threads. Alf brings up a very important issue, and it should be addressed in a reply to this question. - Using **auto** can break programs by letting coders accidentally get hold of types they were never meant to see. – UncleBens Dec 08 '11 at 13:15
  • Alf: Would you please provide a test case of what you are explaining? I’m not sure I understand it clearly, and that sounds interesting. – qdii Dec 08 '11 at 13:29
  • An example: http://ideone.com/NzJBR. The problem is: in C++03 the returned value could only be used as an unnamed temporary. It would have been safe to fill it with references/pointers to other temporaries. But in C++11 you can create a named instance of it. The pointers/references in X::Nested become dangling, but the named instance lives on! - This should not affect lots of code, but it shows that indiscriminate use of auto can break designs that are fine by the C++03 standard. – UncleBens Dec 08 '11 at 16:54

1 Answers1

3

My personal experience is auto is handy for generic code, or things like range-based for loop, but you might get something like

auto count = getCount();
if (count < 0) {
  // do something
}

If getCount() returns an unsigned number, instead of what you might be expecting (int), you won't even get a warning.

Ralph Zhang
  • 5,015
  • 5
  • 30
  • 40