Live data from Hacker News

C++ 11 Auto: How to use and avoid abuse

acodersjourney.com

21–30 of 47 posts

Re: C++ 11 Auto: How to use and avoid abuse

#22

So auto allows you to deal more easily with c++ crap, especially STL. My first thing to do in all my c++ project is to import my in-house STL wrapper, which makes STL usable for me. I never understood how people can work with raw STL. I don't like at all working with templates and i am using it only in internal class implementations. C++ without templates is so much more beautiful.

Would you mind giving some examples? I don't find STL to be crap at all.

Re: C++ 11 Auto: How to use and avoid abuse

#23

Will best practice for the auto keyword change when c++ concepts finally arrive? Of the 3 options below, the third seems best to me: SomeReallyLongTypeName* x = foo(); // old-school auto x = foo(); // c++11 pointer x = foo(); // c++17 concepts

you can already do: auto *x = foo(); Which is actually more readable than "pointer".

Re: C++ 11 Auto: How to use and avoid abuse

#24
post #23

Will best practice for the auto keyword change when c++ concepts finally arrive? Of the 3 options below, the third seems best to me: SomeReallyLongTypeName* x = foo(); // old-school auto x = foo(); // c++11 pointer x = foo(); // c++17 concepts

you can already do: auto *x = foo(); Which is actually more readable than "pointer".

Perhaps a better example be something like:

  smart_ptr x = foo();

Re: C++ 11 Auto: How to use and avoid abuse

#25

I'd be more than annoyed if I saw someone name a function "xxInteger()" because it returns an integer. I don't actually think the first example was that bad, modulo some context. Sometimes even knowing the type for this kind of thing isn't super important to understand what the code is doing; ala opaque types.

What exactly is annoying? I always want my variable names to be as long and descriptive as possible. We're no longer in the teletype era and our monitors are huge. With autocomplete there is no reason to keep variable names short

Visual clutter bothers some people more than others. It bothers me a lot.

Re: C++ 11 Auto: How to use and avoid abuse

#26
post #20
post #18

No. In Herb Sutter's words, avoiding auto since it makes the code "unreadable": ...reflects a bias to code against implementations, not interfaces. Overcommitting to explicit types makes code less generic and more interdependent, and therefore more brittle and limited. It runs counter to the excellent reasons to “write code against interfaces, not implementations” See http://herbsutter.com/2013/08/12/gotw-94-solution…

Why this dismissive comment? auto is definately overused in ways that are unreadable, exactly like the blog author says. Marking the type explicitely may be less generic in a way, but it helps readability so much by providing redundancy.

The author only gives two problematic usage examples, the first of which (ConjureMagic) nercury correctly addressed earlier as a symptom of poor OO design (related to the 'code to implementations' quote above).

The second issue (async) is quite similar in my opinion, and in practice your async handler will be explicitly decoupled and strongly typed at some point for reasons of testability so the example is describing also an OO design issue and not an issue with auto.

There may be real problems with auto but this article did not provide any compelling evidence of them.

Re: C++ 11 Auto: How to use and avoid abuse

#27
post #10

Earlier quoted context omitted.

I would be very surprised if someone can meaningfully review C++ templates without an IDE. Really not reviewing a program inside an IDE is a poor idea in general.

Some of us still use emacs and vim.

Surely people using vim/emacs for serious C++ work are going to at least have ctags, and probably something like YouCompleteMe?

Re: C++ 11 Auto: How to use and avoid abuse

#29
post #22

So auto allows you to deal more easily with c++ crap, especially STL. My first thing to do in all my c++ project is to import my in-house STL wrapper, which makes STL usable for me. I never understood how people can work with raw STL. I don't like at all working with templates and i am using it only in internal class implementations. C++ without templates is so much more beautiful.

Would you mind giving some examples? I don't find STL to be crap at all.

std::unordered_map::iterator it = hashmap.begin();

vs

auto it = hashmap.begin();

I find auto useful for cutting down some of the verbosity of templates STL containers, but I can see how over use can lead to code requiring much more referencing if maintaining code that rarely defines types.

Re: C++ 11 Auto: How to use and avoid abuse

#30

I'd be more than annoyed if I saw someone name a function "xxInteger()" because it returns an integer. I don't actually think the first example was that bad, modulo some context. Sometimes even knowing the type for this kind of thing isn't super important to understand what the code is doing; ala opaque types.

I agree. More so in that example where the explicit int return type was used. One can certainly argue that the original function name isn't good enough, but I would hate to see "type names" being appended to functions (reminds me of the old hungarian programming notation for variables).
Post reply on HN