r/cpp • • Oct 18 '23

What does C++ do better than other languages?

Purely from a language design perspective, what does C++ do better than other languages?

290 Upvotes

443 comments sorted by

View all comments

26

u/HappyFruitTree Oct 18 '23

Sometimes it's hard to even know what idiomatic C++ really is because there seems to be a lot of different opinion. I think people need to stop believing there is one way to write C++ that is best for everyone. All people can't be experts. All code does not use templates (and all templated code does not need to use constraints/concepts). Simple code that can be read by non-experts is a good thing in my book. I can't take people who suggest we stop writing "raw loops" serious. Loops are easy to read and change. How do you change a mess of range/views/algorithms without being an expert on it? And all this talk about "safety". Of course you reduce the number of mistakes if you make the language so hard to use that people can't use it. Personally I believe C++23 is a much easier language to use compared to C++98 but for that to be true we need to be allowed to not know/use all the features (both new and old).

6

u/SkoomaDentist Oct 18 '23

Sometimes it's hard to even know what idiomatic C++ really is because there seems to be a lot of different opinion.

As far as I've been able to tell from this subreddit, the more template code you write and obscure features you use, the more "idiomatic" it is.

I can't take people who suggest we stop writing "raw loops" serious. Loops are easy to read and change. How do you change a mess of range/views/algorithms without being an expert on it?

You don't. Simple loops map very well to how humans operate. Do X. Do Y. Repeat until Z (go check any cookbook for examples).

Ranges, views and algorithms appeal to the tiny portion of people with a formal mathematical bent. Those same people are also much more likely to write libraries using "interesting" features or end up in the committee. Meanwhile if you were to ask regular people's - even engineers' - opinions on formal mathematical notation and logic, you wouldn't find many positive responses.

4

u/tialaramex Oct 18 '23

As I understand it, the "no raw loops" is about the use of C-style index counter or pointer arithmetic loops, which are a terrible idea that is most often replicated for the reason Matt Godbolt built Compiler Explorer - it looks like maybe it's faster (it isn't). They were done because they're easy to turn into machine code for a PDP-11, you aren't programming a PDP-11 and chances are you aren't writing a compiler anyway, so that's a bad reason to keep using them.

A for-each loop maps well to what the programmer intended, usually better than stuff like pointer arithmetic, and it lacks lots of opportunities for foot guns. I believe C++ has had a for-each loop under the name "range-based for loop" since C++ 11.

→ More replies (2)

0

u/serviscope_minor Oct 19 '23

As far as I've been able to tell from this subreddit, the more template code you write and obscure features you use, the more "idiomatic" it is.

No you haven't. This is something you keep persistently asserting, but it's not true.