4 ms·
Then start with Herb Sutter, because that is exactly his advice at the latest CppCon.
by RogerL 12y ago
Then start with Herb Sutter, because that is exactly his advice at the latest CppCon.
- jobu 12y agoYes, and he's said that in a few blog posts as well: http://herbsutter.com/2013/06/07/gotw-92-solution-auto-variables-part-1/ http://herbsutter.com/2013/06/07/gotw-92-solution-auto-varia... http://herbsutter.com/2013/06/07/gotw-93-auto-variables-part-2/ http://herbsutter.com/2013/06/07/gotw-93-auto-variables-part... http://herbsutter.com/2013/08/12/gotw-94-solution-aaa-style-almost-always-auto/ http://herbsutter.com/2013/08/12/gotw-94-solution-aaa-style-... He has some good points, but his perspective seems to be biased by a professional focus on writing libraries and more general-purpose code. He's also brilliant and can remember things like this: const int& cir = val; auto h = cir; //The type of h is int. It's difficult to make generic rules for how to use C++ (because it's used for everything from adware to nuclear reactors), but I can say that Herb's AAA (Almost Always Auto) rule would not fly on any development team I've ever worked with.
- swift 12y agoThe type of h isn't so hard to understand, there. |auto| gives you a value; if you want a reference, use |auto&|. This rule actually makes |auto| more readable: you always know if you're looking at a reference or not. Probably you expected |auto| to behave like |decltype()|, C++11's other form of type deduction, which gives you the actual type of an expression. C++14 adds |decltype(auto)|, which lets you declare local variables using this behavior if you want it. I'm not sure whether I'd apply Herb's AAA rule quite as broadly as he suggests, but I think it's broadly right, because far too often in C++ we write a type twice on the same line, which is just redundant. I vastly prefer |auto val = new MyClass;| to |MyClass val = new MyClass;|, and similarly |auto casted = static_cast<MyType>(uncasted);| is completely readable without typing writing "MyType" a second time. C++ programmers are a conservative lot, but I expect using |auto| at least in those cases to be pretty mainstream within a few years.