Live data from Hacker News

C++11 FAQ

stroustrup.com

51–60 of 121 posts

Re: C++11 FAQ

#51

I've been programming for money for over 20 years and I grew up professionally writing C and then half-migrating to C++, because the latter very naturally captured lots of C patterns and allowed for more succinct expression of the same. It was lean, expressive and elegant . Just needed a bit of Lisp-y closures and it would've been perfect. And then things got completely out of hand. Elegance was nuked from the high o…

I feel your pain but the canonical response is that you don't have to use the new features if you don't want to. If you have to work with other people's C++ code you may be in trouble.

I have the same arc as you, but every time I come back to C++ (from C# or Python) I'm surprised how much I enjoy it. I look at the new stuff more as a way to bring features from scripting or functional languages to a real language - makes me happy.

Re: C++11 FAQ

#52

I've been programming for money for over 20 years and I grew up professionally writing C and then half-migrating to C++, because the latter very naturally captured lots of C patterns and allowed for more succinct expression of the same. It was lean, expressive and elegant . Just needed a bit of Lisp-y closures and it would've been perfect. And then things got completely out of hand. Elegance was nuked from the high o…

> ass-backwards closures

that's very surprising. I would say that C++ lambda expressions have been an incredible success and probably the best thing to come out of C++11.

For example, rust started with boxed closures, because that's what all the 'proper' languages do. They were forced to switch to distinctly-typed, unboxed closures with similar capture modifiers to be competitive with C++ [1].

Re: C++11 FAQ

#53
post #2

Good stuff, I can also heartily recommend Scott Meyer's Effective Modern C++ book which covers idiosyncrasies and some unexpected behaviour (at least to me). Also Stroustrup's 4th edition of The C++ Programming Language is invaluable, if not just for the more-readable sans-serif font compared to the 3rd edition! And of course the coverage of all C++11 changes in it... I can also recommend the Overload journal for int…

I need to figure out what to do with my expensive old hard cover addition of the (C++98) C++ Programming Language. It just sits there looking sad. Not much value in it anymore.

It's a monument to progress; for maximum artistic and educational value, place it where you see it every day on top of decorative obsolete hardware (I recommend the Thinking Machines CM-2 for prettiness or SGI workstation for historical value).

Re: C++11 FAQ

#54

Earlier quoted context omitted.

This is perhaps one of the more unexpected opinions on C++ I've encountered. When I picked up C++ with Turbo C++ 3.0 in the early '90s, it was already widely regarded as an over-complex behemoth. Donald Knuth said of it in 1993, "Whenever the C++ language designers had two competing ideas as to how they should solve some problem, they said 'OK, we'll do them both'. So the language is too baroque for my taste." The la…

I started with C++ around '92, but you are right, C++ was already complex back then, but it was still manageable, you could, sort of, hold its larger picture entirely in your head and still had some elegance to it.

As a dissenting opinion from someone who has not quite 20 years of experience with C/C++ but 14, modern C++ is what brought me back to the language. The idioms allow me to use the language more functionally. More importantly, the STL is actually usable now and you can just code using it and std::algorithm without thinking of the nuts and bolts of move assignment and stuff at all. In the case where you need performance, you have the paradigm at your disposal. Also, you have a ref counted pointer thread safe pointer! A means of transferring ownership so heap allocated objects can also follow the same RAII pattern!

I have a much easier time holding the entire picture together but others are correct in saying you have to throw away old conceptions and ideas of the language. I personally have thrown away the way I think about C entirely (at least when I code in modern C++).

Re: C++11 FAQ

#55
post #4

Earlier quoted context omitted.

some of the pain points you refer to, esp move semantics, bring a lot of additional performance to the table. Would you feel the same way about C++ if you started now and only had to learn a clean C++11 subset going forward ?

I've often wondered how anybody manages to teach C++ today. I love working with the language, and I understand the reason behind many of the features, but I can't help but feel like it would be impossible to teach to beginners.

"...but I can't help but feel like it would be impossible to teach to beginners."

So a relatively easy way to learn C++ is as an 'upgrade' to C, starting of with picking the parts of the language that help you the most:

* you start by already knowing C, but maybe not feeling super productive, using it as structs + functions

* you set up the C++ compiler (most modern version available), and just continue writing C

* you start using streams to print (because it can be more convenient than printf)

* you start using std::vector and std::string, arrays being one of the big pain points of C

* you start using `auto` and for-in loops

* you start using simple classes, without any inheritance. just structs with methods

* you start adding operators - so much fun!

* you start learning about references and const, and suddenly all your method arguments become `const T& v`

* you learn about not using pointers (as much on stack as possible), move semantics, shared/unique pointers. This means you'll also use more templates.

* you deepen your understanding of writing your move/copy constructors. Learn RIAA, and become a fan of `}`.

* you learn about constexpr and constexpr functions

* this is already a pretty happy subset of C++, and as you learn to use templates, you may at some point start writing one yourself. Same with lambdas - if you need them you learn them.

Re: C++11 FAQ

#56

I've been programming for money for over 20 years and I grew up professionally writing C and then half-migrating to C++, because the latter very naturally captured lots of C patterns and allowed for more succinct expression of the same. It was lean, expressive and elegant . Just needed a bit of Lisp-y closures and it would've been perfect. And then things got completely out of hand. Elegance was nuked from the high o…

But C++ was never easy to understand. For example, can you tell me off the top of your head, what are the conditions to get the compiler to generate a copy constructor and an assignment operator for a class? I have trouble answering that question even with the help of Google, and you're talking pre-Google times.

The rules really aren't that hard for practical use. Of course pedantic rules in the standard are much more complex and corner cases can bite us.

If you define no constructors then the compiler will generate a default constructor with no args, a copy constructor, a move constructor and an assignment operator. If any class data members don't have one of those then the class you are writing won't have one either.

The simplest practical rule is define none of them or define all of them. It is not often, perhaps one in ten classes, that you should do something else.

If wind up using C++ for more than a small project, than you ought read up and not use simple heuristics like this.

Re: C++11 FAQ

#57

Earlier quoted context omitted.

My personal main problem is things like you can't template on an r-value reference, as T&& in a template matches both r-value and l-value references and then you need std::forward or std::move and then I start getting confused. I wish in templates they had gone with the suggestion I saw to use T&&& to mean "r-value or l-value reference", and then let T&& just mean "r-value reference".

If you want to template on an r-value reference, the easiest way is probably to use a forwarding reference and disable the template if the template parameter deduces to a reference template ::value>> auto f(T&& x);

Is this C++14? Can you please explain the "class = ..." template parameter?

Re: C++11 FAQ

#58

Earlier quoted context omitted.

How well has Perl fared? How about Lisp and Scala that are multi-paradigm, how well are they doing?

Perl was doing just fine until they fell in the perl6 hole and stagnated. Scala's getting its data science niche, and lisp has a lot more problems than just being multi-paradigm.

> Perl was doing just fine until they fell in the perl6 hole and stagnate

i.e. backward compatibility is king.

Paraphrasing Stroustrup, there are two kinds of languages, those that are full of old cruft and those that are too green to have been proven in the field.

Re: C++11 FAQ

#59
post #34

Earlier quoted context omitted.

Move semantics are very useful, but there's also something wrong if articles like this are necessary or even possible: https://isocpp.org/blog/2012/11/universal-references-in-c11-...

Scott Meyer's articles (and numerous video presentations) on Universal References are educational but he doesn't make it clear to the audiences the bigger picture of where and why they'd need to really learn it. The Forwarding Reference (aka Universal Reference) is for programmers writing generic templates . A lot of C++ programmers can do a ton of productive work and get by for years without writing any templates fr…

" A lot of C++ programmers can do a ton of productive work and get by for years without writing any templates from scratch" as well as people who choose not to have a stove or oven in their kitchen can eat, but with the delusion that they are smartly avoiding difficulties.

Re: C++11 FAQ

#60

Earlier quoted context omitted.

That was the thing: starting with C++98, I was able to pick up C++11. But starting from scratch? I'm not sure.

Wow I feel the opposite. Because C++98 and earlier were so deficient in a lot of ways, people who got clever with it often ended up down a rabbit hole of template metaprogramming, boost:: bloat, custom classes for callback stuff, custom classes for reference counting or other smart pointers, etc. etc. Unlearning all that took me some time. In my 5 years at Google, I've watched many developers start from scratch on ou…

Is the subset of boost in Google still the same as the public one?

https://google.github.io/styleguide/cppguide.html

It it is, I consider Google very lucky to have these who decided not to allow everything.

Post reply on HN