5 ms·
Not to focus on a question unrelated to this blog post's point, but * "auto main() -> int" could be just "int main()". * "auto v = vector<int>(20);" could be
by jlebar 12y ago
Not to focus on a question unrelated to this blog post's point, but
* "auto main() -> int" could be just "int main()".
* "auto v = vector<int>(20);" could be just "vector<int> v(20)".
* "auto print_value = [](auto&& v)" could be (I'd argue should be) "auto print_value = [](const auto& v)".
"auto" is useful, "auto" is great. But "auto" is not an end in and of itself.
(Bring on the C++(11) haters, blah blah.)
- forrestthewoods 12y agoI'm not convinced auto should ever be used for anything other than iterators and anonymous types. There are other instances where it's relatively fine to use. Perhaps a few where it's even slightly better to use. But in C++11 I think it's by far best to limit it to there be explicit cases I mentioned. In C++14 I'll extend support to lambdas in some cases but will need to experiment to say for sure.
- fafner 12y agoSorry, but your comment lacks substance because you don't provide any argument for the way you think. Why should it only be used for iterators and anonymous types? There is a pretty good argument for using the auto var = T{val}; style of defining variables. The {}-initialization won't narrow literals. You can find more discussion about this in GotW. Maybe you have a good reason for not liking the style and there certainly can be arguments against it. But from experience I find that many people opposing more use of auto simply oppose it because it is different to their old ways. Therefore I'd like to see a proper argument for your comment.
- EliRivers 12y agoI really, really like knowing what kind of object something is by looking at the code where it is created. I really, really like it. I find it extremely helpful. This sort of thing: auto x = function(); is so unhelpful I find it frustrating and an active impediment when I can look at an object being created and not know what kind of object it is. In some programming languages, knowing what kind of object something is doesn't matter so much. Not the case here.
- fafner 12y agoWhy do you need to know the exact type? Why does it matter so much for C++ but not for other languages? I mean there are templates in C++ and a lot of code even in C++98 was written not knowing the exact type and just assuming or expecting certain properties/methods to work.
- EliRivers 12y agoWhy do you need to know the exact type? Okay, YOU tell me what class functions that variable "x" has. Code isn't written just for the compiler to read.
- forrestthewoods 12y agoI specifically referred to C++11. And I believe your example with the T{val} is C++14 so I don't know much about it. My experience is working on video game engines with other people. As a senior developer a large part of my job is jumping through a wide range of systems written by other people to debug problems, identify ways to increase performance, add features, etc. There's also a lot of code written by people who have moved on to other jobs. We've had quite a bit of code that was full of autos. In my experience thus far auto has never made code more significantly readable or easier to understand than not using auto. (excluding iterators/lambdas) Not only has auto not made code easier to comprehend the use of auto has made it significantly more difficult to understand on more than a few contexts. If deducing a type as a human reader of text requires backtracing a half dozen calls of code across who knows how many lines of code, and even files, then I'm gonna be justifiably grumpy. It's a huge burden. And for what benefit? Damn near nothing in my experience so far.
- MrDosu 12y agoThis is something an IDE does for you...
- forrestthewoods 12y agoIn theory, but not in practice. Visual Studio starts off great but it eventually chokes and dies for C++. Tools like Visual Assist can extend the lifetime but eventually it too will fail. This is true for every game and every engine I've ever worked on. I now do 100% of code editing in Sublime Text. Other people use other text editors. I'm now of the opinion that code bases should be useable and searchable in plaintext form. It's not difficult and even with an IDE makes things better imo.
- MrDosu 12y agoI would (almost) completely disagree with this. C++ support in VS is horrible, that's correct, but having the ability to be supported by compiler services that can parse (invalid) code is a major milestone when it comes to handling more complex codebases. It does not matter how smart you are, the easier it is for you to understand and reason about the code, the more will fit in your head. Refactoring is just one of the many amazing tools that make me a much better and more productive programmer today then I was without when using vi in the 90s. Add in static analysis, intellisense etc... Every bit of complexity that tooling can hide from you is worth gold. Reality for C++ is grim though in this regard. Let's hope we get better compiler services for it soon.
- papaf 12y agoMost people think that auto should not be used much, but Herb Sutter recommends that everyone should use it much more. His argument is convincing: https://github.com/CppCon/CppCon2014/blob/master/Presentations/Back%20to%20the%20Basics!%20Essentials%20of%20Modern%20C++%20Style/Back%20to%20the%20Basics!%20Essentials%20of%20Modern%20C++%20Style%20-%20Herb%20Sutter%20-%20CppCon%202014.pdf?raw=true https://github.com/CppCon/CppCon2014/blob/master/Presentatio...
- fafner 12y ago1. The new declaration style might look a bit strange to people used to the old style. But one could argue that using it will make it more consistent for the cases when you really do need to use it. Anyway it's just a matter of style. Not really a need to discuss it IMO. 2. Some people argue that auto v = T{x}; should be the new way of defining variables with types. The use of {}-initialization should prevent unnecessary narrowing. In this case () has to be used because a specific ctor is to be called. But using that style would make it more consistent with the rest.