r/programming • • Apr 01 '13

Ten C++11 Features Every C++ Developer Should Use

http://www.codeproject.com/Articles/570638/Ten-Cplusplus11-Features-Every-Cplusplus-Developer
469 Upvotes

283 comments sorted by

View all comments

Show parent comments

12

u/wilhelmtell Apr 02 '13 edited Apr 02 '13

auto is not there to save keystrokes. Occasionally it's an added bonus, sometimes you actually type more than otherwise. auto is a tool to get stuff done and to avoid traps, not a mere syntactic helper.

  • You can use auto for return-type computation as a function of the signature.

  • You can use auto as a DRY tool.

  • You can use auto to make conversions explicit. Without auto conversions in initialization were implicit and there was no way for you change that unless you could change signature on the right side of an assignment. In the case of builtins -- not even then.

If you don't know the type on the right side of an assignment you look it up. You don't duplicate stuff "to relief others from looking them up". You don't copy-paste entire friggin functions to relief yourself or your poor descendants from looking up code. You don't repeat function declarations on top of your code to relief your colleagues from looking up the signature.

1

u/sol_aries Apr 02 '13

I dont understand how 'auto' helps to reduce code repetition, or how not using it leads to copying and pasting functions. Can you explain further?

6

u/wilhelmtell Apr 02 '13 edited Apr 02 '13

I didn't say that not using auto leads to copy-pasting functions. I motivated my point against redundancy by analogy with duplicating code. You won't duplicate code "to make it easier to find", and hence the argument against auto for "hiding information" is not valid.

As for auto reducing redundancy: when you say 42 you're expressing two things: a value and a type. If you also specify a type on the left side of an assignment and that type is the same as that on the right hand side of the assignment then you pose redundancy. You say the same thing twice. That's bad.

If, on the other hand, you specify a type on the left hand side of the assignment different from that on the right hand side of the assignment then you are expressing a conversion. This is not redundancy, and it's perfectly valid and meaningful to do.

Up until this standard you had no choice. You used the same syntax for initialization and for conversion. Now with auto you have a choice, and so now (generally speaking) when I see an initialization without auto I immediately assume it's a conversion. If it's not a conversion then, like others before me here have said (albeit to motivate the contrary) I'd be annoyed. Little is more annoying than code that lies, like a comment that disagrees with the code, or a misnomer, or a conversion that isn't.

I say "generally speaking" because there are times there's no need to be anal about this. For example,

for( int i = 0; i != count; ++i ) { ...

I know you mean to loop and I know int i = 0 does not express a conversion.

0

u/sol_aries Apr 02 '13

What about auto i = foo()? 'i' could be a window handle, a file pointer, a class, etc. Not knowing instantly, intuitively, visually, even subconsciously is a detriment to you and all who interface with your code. Even if it only takes 2 seconds to figure out the type of the auto, it over hundreds/thousands of lines of code, that would be slowing me down significantly.

4

u/wilhelmtell Apr 02 '13

If you don't know the return type of foo() then maybe the problem is in foo; the name choice perhaps. Suppose the line was

grunt i = foo();

Does this line tell you what the type of foo() is? You might think it does, and if you got it wrong then we have an issue here: the line mislead you.

The truth is the line does not tell what the type of foo() is. At most, it misleads the uninitiated. All it says is the type of i. If you want the type of i to be grunt then that's perfectly fine. If you want i to be the type of foo() then it's not fine; you should ask for that instead, with auto. It all boils down to what your intent is.

-4

u/sol_aries Apr 02 '13

'grunt' is one step closer to the answer and provides one less file you have to open than 'auto' and it might be a sufficient answer by itself.