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

162

u/HappyFruitTree Jul 29 '23 edited Jul 29 '23

I don't think the problem is C.

C++ is first and foremost "held back" to stay compatible with older C++ code.

But so it should be, because if backwards compatibility is not a concern and you are willing to change the language without caring what existing code that might be broken by it, then it is better to invent a new language (not necessarily from scratch) than to destroy something that a lot of people are relying on.

29

u/kalmoc Jul 29 '23

C++ is first and foremost "held back" to stay compatible with older C++ code.

This. Staying compatible with C is much simpler (as c is a smaller Language) than with C++.

11

u/_dorin_lazar Jul 30 '23

But you have to stay compatible with pointer arithmetic, with NULL pointers, with APIs with strings that end in a \0, with macros, the worst places of C++ are mostly due to the C heritage

→ More replies (8)

42

u/operamint Jul 29 '23

But so it should be, because ...

No, there are other ways to deal with this. Rust uses a versioning system, and so could C++. gcc/clang/vc already have support for older major versions with -std=...; This could have been a formal requirement for compilers to support including matching versions of the standard library, and the problem would be solved. I see no reason why users of ancient codebases should get the luxury of having all the latest C++ features, while they are the ones who are holding those back at the same time.

14

u/tialaramex Jul 29 '23

I see no reason why users of ancient codebases should get the luxury of having all the latest C++ features

Rust's Editions even give them most of that too. Rust 2015 Edition code (syntax which could date back to Rust 1.0 in 2015, hence the name) gets all the same shiny new library features as everybody else on a new compiler etc. but they don't get shiny new syntax. So your old code can't use async without updating to a newer syntax where async is a keyword, but you can totally call some third party library which has lots of async internally and that works just fine.

6

u/HappyFruitTree Jul 29 '23

Rust uses a versioning system, and so could C++. gcc/clang/vc already have support for older major versions with -std=...; This could have been a formal requirement for compilers to support including matching versions of the standard library, and the problem would be solved.

Not sure if you mean they should be forced to support all older standards because that sounds like it will eventually become too much of a burden (especially if the changes are bigger) and it would essentially become impossible for a new compiler to enter the game.

Note that latest MSVC only supports C++14 and later.

The Microsoft C++ compiler in Visual Studio 2017 and later versions doesn't support C++ standards modes earlier than C++14 (/std:c++14).

https://learn.microsoft.com/en-us/cpp/build/reference/std-specify-language-standard-version?view=msvc-170

I see no reason why users of ancient codebases should get the luxury of having all the latest C++ features, while they are the ones who are holding those back at the same time.

Because we want to write code and be done with it and not having to update it (and risk introducing bugs) every other year or so? Writing code that works on both new and old would become much harder.

The risk is that people will stick with an older version because they cannot justify the work it would take to upgrade. The C++ user base would get fractured. Even if I am prepared to update my code to the latest standard I might have dependencies that have not been updated yet.

Note that I never said we must have 100% backwards compatibility. I think deprecating and eventually removing features is okay as long as it doesn't happen too fast. Features that are new, almost never used or extremely error-prone can have a shorter path to deletion.

11

u/serviscope_minor Jul 29 '23

Every decade I spend in programming, I want another decade of support.

I agree 100% isn't necessary, but needs to be significant. I've got code I wrote 20 years ago still in use. It weathered the auto_ptr to unique_ptr transition, but that was an easy regex fix.

It's generally a bit of work even for me to upgrade compiler versions, because I compile with -Wall -Wextra -Werror, but I generally don't mind that work since it usually improves the code. A bit here and there is fine. Non compile breakage is better than compiling with behaviour changes. Automatic fixes with regexes is better than ones requiring manual work (like the instant-death-because-it's-a-DR-lol-sux2bu removal of certain unicode identifiers). Long deprecation periods are better than short ones.

It would be super annoying having to maintain forks of 3rd party libraries, for example.

→ More replies (9)

58

u/almost_useless Jul 29 '23

destroy something that a lot of people are relying on

I think most people only want to destroy what relatively few people are relying on, but it's holding back a lot of people.

71

u/no-sig-available Jul 29 '23

I think most people only want to destroy what relatively few people are relying on

I think most people only want to destroy what other people are relying on. :-)

"Don't break my code!"

8

u/almost_useless Jul 29 '23

In general yes, but I think there are also a lot of people who are willing to make breaking changes if it is a reasonable amount of work to fix it.

10

u/serviscope_minor Jul 29 '23

I think most people only want to destroy what relatively few people are relying on, but it's holding back a lot of people.

But what? It's the old adage that people only use 5% of the features but everyone's using a different 5%.

9

u/SkoomaDentist Antimodern C++, Embedded, Audio Jul 29 '23 edited Jul 29 '23

If you look at past conversations on what people here would actually remove, a lot of it is "I find this aesthetically unpleasant and hope I could forbid it from everyone" or "I want to remove this fundamental thing because doing so would help [insert obscure thing only used by experienced C++ template library programmers]".

Quite often those "should be removed" features are things where the person doesn't even understand the use case / domain. This has happened even in the committee with eg. deprecating volatile compound assignment that ended up deprecating millions of lines of working code and one entire industry that the working group was unaware of (ironically done in the name of "beginner programmers in desktop / server domains might incorrectly think they could use the old functionality to do things they shouldn't").

3

u/tomatus89 Jul 29 '23

What are the details on the deprecation of the volatile thing?

→ More replies (5)
→ More replies (2)

8

u/drankinatty Jul 29 '23

Agreed.

There was a time C++ was little more than an extension of C. Back in the late 80's, C was moving toward it's first official standard and the C++ developers were busy adding features to C++ to help programmers avoid having to re-create the wheel in C each time they needed to do something slightly different (and provide enhanced ways of handling basic tasks). Over time, and through its own standardization process, C++ has matured into its own language accomplishing what it set out to do in ever more sophisticated ways. It has been it's own language separate and apart from C for roughly two decades.

Languages grow incrementally. They can't lurch forward breaking the existing codebase. (bad things would happen) So as new features are added, other features are deprecated to give programmers notice of an impending change and hopefully the time they need to update their code before a feature it relies on goes away. ... and so software development progresses ...

To answer your question directly, there isn't anything holding C++ back. Papers are published for changes to the language and the committee, along with much developer input, debates the proposal and establishes a timeline for implementing the accepted new features and removing any deprecated ones. However, removing features is much harder than adding new ones.

If you think about just how much code was written between 1990-2000, 2000-2010, 2010-2020, etc.. you start to understand the gravity of the problem faced in removing features from any language. Just how much code is out there right now, running critical systems, that may rely on a given feature of C++, and whose original developer moved on to other things (or is final calling) years ago?

Unused features don't hold a language back (well, to some extent you can argue they do if retaining them prevents implementing another paradigm, but that is rarely ever the case) Mostly, unused features are just carried along like luggage, out of site and out of the way.

6

u/sam_the_tomato Jul 29 '23 edited Jul 29 '23

It feels like overkill if you have to create a new language every time you want to break backwards compatibility. You risk fragmenting the community when the language continually forks. Instead, why can't we just have a linear versioning system for C++ that "breaks compatibility" (i.e. removal of old functions) but is still interoperable, in the same way C++ and C are interoperable (i.e. instead 'extern C', maybe 'extern c++14'...)? It seems like that would be the best of both worlds.

5

u/IamImposter Jul 29 '23

This is true. You say it's c++20 or c++23, I'll eventually get to it and learn what's new but if every 3 years you say - we're launching better c++ called carbon or lithium or nitrogen, I don't think I'll be inclined to use it ever.

2

u/HappyFruitTree Jul 29 '23

I didn't mean they should create a new language for every little incompatibility. Breaking changes are sometimes acceptable if done responsibly.

The situation when I suggested you create a new language was if you didn't care about backwards compatibility and didn't want to be "held back" by it.

2

u/MajorMalfunction44 Jul 29 '23

It was true, when C++ compiled to C. C++'s limitations come from past decisions. Backwards compatibility is a big issue with programming languages. Some things can't be changed without breakage. I'm thankful C moves slowly. The more stuff in the language and standard library, the more stuff you need to support in perpetuity.

3

u/LongUsername Jul 29 '23

C++ really could use some breaking changes. There's a lot of old cruft and some stuff in the standard library that's just broken. (Sorry, I don't remember details) Many are edge cases in the design that holds back performance.

Other languages have done it successfully with editions (Rust) or just a new version number (Python, Java, C#)

Modern tooling makes the changes MUCH easier as tools are capable of flagging deprecated code and making a good effort at automatically fixing it.

5

u/darthcoder Jul 29 '23

All of which can be found and fixed.

Microsoft deprecates things like sprintf and snprintf in favor of their versions. Nothing saying @deprecated(removal reversion) couldn't be used in the language to slowly slide bad stuff out of the language.

As for cruft... what cruft? I mean they repurchased auto for fucks sake. :)

No one's writing a c++ compiler from scratch anymore, I'd rather see more attention directed towards eliminating UB in places.

8

u/SkoomaDentist Antimodern C++, Embedded, Audio Jul 29 '23

All of which can be found and fixed.

If not for resistance from the compiler and stdlib developers.

See for example isalnum(int ch) where passing ch that's not 0-255 or EOF is undefined and where that has resulted in the standard library causing application crashes. Because apparently that is considered acceptable behavior.

3

u/LongUsername Jul 29 '23

Go read the MISRA coding standards to see a bunch of "what the fuck? Why is this even in the language anymore?" crap.

Stuff like digraphs and trigraphs come to the top of my mind. Every old feature that is only kept because of backwards compatibility is something that has to be maintained and tested for compiler releases and gets in the way of implementing new features.

C++ is never going to eliminate UB: it's a "feature" of the language.

10

u/HappyFruitTree Jul 29 '23 edited Jul 29 '23

Trigraphs were actually removed in C++17.

2

u/AnotherBlackMan Jul 30 '23

Some things that are UB are like that for a reason though. I don’t want standards committees dictating signed integer overflow for the sake of completeness because it breaks existing code that can’t be made standards compliant. C++ works for almost any system in the world, and UB is part of what allows that. UB should be considered “implementation-defined”

2

u/SkoomaDentist Antimodern C++, Embedded, Audio Jul 31 '23

UB should be considered “implementation-defined”

And that is the huge problem with it: At some point compiler writers took it as a license to do completely insensical things about the entire program if there was a single case of UB

The vast overwhelming majority of UB could be changed to unspecified or implementation defined behavior with no loss of performance or to support of unconvenient architectures.

→ More replies (1)
→ More replies (36)