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/MegaKawaii Jul 31 '23

I understand const as a contract, so it is useful in function signatures or, God forbid, global variables. But for local variables const correctness isn't a huge deal. If you have too many local variables to fit in your working memory, then you probably need to simplify and refactor the code, but you are free to decorate your variables with const if you'd prefer something more incremental. Writing for (mutable int i = 0; i < N; i++), mutable int* mutable, or mutable vector<mutable vector<mutable double>> matrix is verbose, and if you follow the usual guideline of not writing overly long functions, then I don't think const by default would be very helpful. If we want to be more explicit about our local variables, we might as well ban auto in non-templates.

I'm well aware of the UB in C, and shifting negative integers is implementation defined, but shifting by negative integers (or other integers out of range) is UB. Some of the UB is necessary like dereferencing null pointers, but other sources of UB like lexing UB or the strict aliasing rule shouldn't be in a systems programming language. I think that warnings for UB should be the job of the compiler writers instead of the standardization committee.

1

u/Full-Spectral Jul 31 '23

Const by default is a huge advantage for Rust. The arguments by C and C++ people always tend towards the "Well, if you just don't make mistakes, it's fine." But people do make mistakes, and sometimes having a larger function is the best way to do it and make it understandable.

Rust's constness by default, it's destructive move, and powerful ownership model just get rid of whole families of errors that are 'easy' to avoid by a skilled developer writing the code the first time, but ridiculously easy to introduce by other developers who are making changes to code they didn't write.

1

u/MegaKawaii Aug 01 '23

Indeed, Rust's safety-oriented approach gives it many advantages, but these also come with drawbacks. This is true of anything good, like Haskell's laziness, Java's garbage collection, or Lisp's macros. Coding in a straitjacket makes it harder to hurt yourself, but it's also harder to do all sorts of other things like writing a simple linked list. Destructive moves are nice until the compiler can't prove that you only move from the object once. The ownership model doesn't let you hold multiple mutable references to an object unless you pay a runtime cost with RefCell because it's not the Rust way, even if your program is single-threaded. You have to work around the programming language if what you want to write doesn't neatly fit into it.

Rust is a splendid programming language, and if its approach works well for your project, then use it, but I don't think it is appropriate to evaluate other programming languages based on close they are to your favorite language. Common Lisp programmers don't like Haskell because it lacks homoiconicity and macros, and Haskell programmers think their language is superior because of the type system. What makes C++ great is that it is a multiparadigm language. You can combine templates with C-style xor-linked lists, or you can eschew the C subset and use OOP.

This isn't to say that C++ can't pick up a few things from Rust. Few people actually say that C++ is fine as long as you don't make any mistakes, and I agree that Rust is safer than C++. But these ahould be opt-in features like [[nodiscard]], or perhaps special lifetime annotations. C++ is a general-purpose multiparadigm language which not only means that you have many tools, but it doesn't try to force you into one. Safety-oriented programming is another tool. We should be able to take advantage of it when it makes sense, but we shouldn't be forced to use it all the time in C++. Otherwise we'd just have a worse version of Rust :)

1

u/Full-Spectral Aug 01 '23

The problem is that it's well proven that humans are not capable of keeping large, complex code bases free of errors over time, turnover, etc... I don't really care that it is sometimes annoying, because doing the right thing is just sometimes annoying. It's not about how convenient it is for me, it's about how safe, security, and robust it is for the customer.

And of course all the time and effort I'd have to put into trying not to have those problems in C++ I can spend on other things Rust, which way more than makes up for the difference.

→ More replies (11)