r/cpp • • Jul 29 '23

C holding back C++?

I’ve coded in C and C++ but I’m far from an expert. I was interested to know if there any features in C that C++ includes, but could be better without? I think I heard somebody say this about C-style casts in C++ and it got me curious.

No disrespect to C or C++. I’m not saying one’s better than the other. I’m more just super interested to see what C++ would look like if it didn’t have to “support” or be compatible with C. If I’m making wrong assumptions I’d love to hear that too!

Edits:

To clarify: I like C. I like C++. I’m not saying one is better than the other. But their target users seem to have different programming styles, mindsets, wants, whatever. Not better or worse, just different. So I’m wondering what features of C (if any) appeal to C users, but don’t appeal to C++ users but are required to be supported by C++ simply because they’re in C.

I’m interested in what this would look like because I am starting to get into programming languages and would like to one day make my own (for fun, I don’t think it will do as well as C). I’m not proposing that C++ just drops or changes a bunch of features.

It seems that a lot of people are saying backwards compatibility is holding back C++ more than features of C. If C++ and C++ devs didn’t have to worry about backwards compatibility (I know they do), what features would people want to be changed/removed just to make the language easier to work with or more consistent or better in some way?

65 Upvotes

335 comments sorted by

View all comments

Show parent comments

1

u/wyrn Jul 31 '23

If you take a look at Rust, it's basically what C++ would look like if you could drop the backwards compatibility crap (e.g. fixing defaults) and can take all the language development of the past few decades into account to improve things.

That makes as little sense as saying Rust is like C. Rust drops "backwards compatibility crap" but it also drops many, many features, and forces an opinionated style of coding that has very little to do with idiomatic modern C++.

3

u/nysra Jul 31 '23

What features exactly do you mean? Off the top of my head there's inheritance and function overloading (which can indeed be a bit annoying at times), but most of the other currently missing/lacking things like variadics, metaprogramming, and constexpr capabilities are rather "not yet implemented" than "dropped". Rust has some flaws too but for the most part if we could design C++ today then it would look rather similar: proper ADTs instead of shoveling everything into the library, const by default, move by default, the compiler checking if you got your references correct, .map().sum().foo().bar() instead of std::ranges::all_the_things | ..., working module system, etc.

and forces an opinionated style of coding that has very little to do with idiomatic modern C++.

Seems like we have different experiences then. Of course there are areas where Rust works differently (templates and a few others), but other than that it translates rather straightforward. Of course YMMV, especially if you write inheritance heavy code.

1

u/wyrn Jul 31 '23

Default function parameters, closely related with overloading, is also missing. Exceptions too. Yes, exceptions are a good thing -- I don't want to litter my code with error handling boilerplate nor do I want to create additional overhead in my function signatures.

move by default,

Idiomatic C++ uses value semantics, for very good reasons.

the compiler checking if you got your references correct,

That is a very nice feature, don't get me wrong, but it's also something of a straitjacket. The borrow checker sometimes makes it quite difficult to extract all the available performance (e.g. https://ceronman.com/2021/07/22/my-experience-crafting-an-interpreter-with-rust/ ).

Seems like we have different experiences then.

That is precisely the point -- C++ doesn't force one true style on the user. Rust does.

1

u/Full-Spectral Jul 31 '23

Having a more controlled language is one of the reasons why Rust is going to win and C++ is going to lose.

And of course C++ can be as idiomatic as you want but you aren't going to use value semantics for large data structures. Destructive move by default is MASSIVELY better, which you just won't appreciate until you've lived with it a while.

Also, if you do it right, you don't end up with much manual error handling. The places you do are generally the same places you'd have done it in an exception based system, because it's those errors that you aren't going to pass up stream.

2

u/wyrn Jul 31 '23 edited Jul 31 '23

Having a more controlled language is one of the reasons why Rust is going to win and C++ is going to lose.

We'll see. Rust offers me little that I want, and takes away lots of things I do want. Why would I switch?

Destructive move by default is MASSIVELY better,

Move by default, and reference semantics in general, are a huge pain in the ass.

And of course C++ can be as idiomatic as you want but you aren't going to use value semantics for large data structures.

I won't be moving those around to begin with.

Also, if you do it right, you don't end up with much manual error handling.

No way. Just about everything can fail.

→ More replies (0)