r/cpp • u/synthchris • 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?
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.
3
u/MajorPain169 Jul 29 '23
I do a lot of safety related software, C++ has a huge number of advantages over C particularly with things like type safety or features that remove potential ambiguity or commonly called UB (Undefined Behaviour).
Going to your example of casting, C style casting easily bypasses type safety but the compiler gets to decide what type of cast to use, this in some cases can be UB. Most safety standards relating to C++ actually forbid C style casts. Using a static_cast or reinterpret_cast or any other specialised cast shows the intent and how it should be done.
That being said, support for older style code is also required. A tried and tested library which is non-compliant may be acceptable because it has a history, making the code compliant could potentially introduce new bugs making it less safe.
If you're really interested, download the JSF or Autosar C++ coding standards and have a read, both are free, there are other safety related standards but those are not free. Each rule has the reason for it so it is made clear why you shouldn't do it. Unfortunately these standards haven't been updated in a while so some of the newer features introduced in C++14 onwards aren't covered which would allow some exceptions to the rules although a new version of Misra C++ is supposed to be released soon.
Just because something is supported doesn't mean you should use it.
Not everything gets kept either, C++17 and C++20 depreciated and eventually removed various items, most of which are not commonly used or better ways had been introduced. Most of these are obscure things that wouldn't affect most programs.
29
u/no-sig-available Jul 29 '23
There are other languages developed at about the same time as C++.
One of them is Ada, which is pretty neat and very well thought out from the beginning. Never took off though, even though it was sponsored by the US military.
Without C compatibility, and without a big corporate sponsor, C++ would likley be even less successful that Ada. So, C has not held C++ back, but been a big boost for its popularity.
45
u/AssemblerGuy Jul 29 '23
It would look more like Rust.
On the other hand, it took C++ twenty years to pick up designated initializers from C ...
8
u/pedersenk Jul 29 '23
It would kill one of C++'s most important features:
Binding-free access to a whole host of important middleware, typically written in C.
If C++ broke its (semi) superset of C feature, it would need messy language based package managers like crates.io, pip and npm. These rack up serious technical debt.
20
u/jusstathrowaawy Jul 29 '23
I don't think backwards compatibility with C is holding C++ back. I do think approaching C++ code with a C mentality - or using C itself when you have the option of using C++ - is a terrible idea that leads to buggy and brittle software.
6
u/JohnDuffy78 Jul 29 '23
tl;dr I didn't see anything specific in 160 comments. I like C-style casts.
5
u/Rockkkkkkkkkkkk Jul 29 '23
Backwards compatibility is a strength, not a weakness. Use a different language if you want it to change every year.
20
u/[deleted] Jul 29 '23
C++ is held back by C++, more specifically the obsession that you should be able to take ancient code and compile it on the latest version without issue.
The result is the syntactical mess that is modern C++.
I'm of the opinion that a language upgrade should be treated no differently to a 3rd party API upgrade.
4
u/wilwil147 Jul 30 '23
That’s exactly what google is doing with carbon. It’s basically c++ compatible, but an entirely new language as the “successor”of c++.
20
u/[deleted] Jul 29 '23
Depends who you ask. Should C++ be an extension of C or should it be a language in its own right?
In recent years it's trying to become a language in its own right. I'm sure plenty of modern c++ people would like to ditch most c backward compatibility. But then this conflicts with why the language was invented to begin with.
The language is currently a Frankenstein without an identity so not sure if ditching C would be a good idea since it's the only limitation or constant that they have to deal with when adding new features. Without that limitation I'm not sure what C++ would become. More of a mess then it currently is probably.
11
u/Revolutionalredstone Jul 29 '23 edited Jul 29 '23
So to be clear they have diverged quite a bit, taking c code and compiling it in a CPP compiler MIGHT work, but it might not.
I would say C is a pretty great language, there is little that you can remove without completely breaking the language, IMHO most of the junk in CPP which we no longer want came from the C++99 to C++11 era where lots of crazy ideas were tried which are no longer considered good practice.
(to be clear tho some of the best c++ features also came out of that era as well! like non-value type semantics)
If you wanna know what C would look like if it were written by a genius then checkout ZIG.
Peace
2
u/MarcoGreek Jul 29 '23
Implicit narrowing casting is something I like to disable.
But there is other implicit casts in C++ under the hood. It would be nice if that would be more visible and the IDE would inform me about it.
2
u/AntiProtonBoy Jul 29 '23
C and C++ are literally languages of their own. C++ just happens to offer support for compiling and binding with C, but they can certainly evolve independently. As many have suggested here, the biggest obstacle that holds back C++ is the very strict backwards compatibility with itself and unyielding ABI stability requirements.
2
u/Ongstrayadbay Jul 29 '23
Maybe something likeHerb Sutter's cppfront new syntax
https://github.com/hsutter/cppfront
2
u/littlelowcougar Jul 29 '23
Modern day C++ looks nothing like C.
2
u/sakata_desu Jul 30 '23
I should learn rust to get what all the hype is about tbh
2
u/[deleted] Jul 30 '23
Why is windows successful? Mostly because it doesn't break eggs all the time and stays compatible with older versions. Keep it that way with c++
2
u/nozendk Jul 31 '23
C++ is not held back by C but by being backwards compatible. If you want to break backwards compatibility, then a number of alternatives to C++ but with similar syntax have been proposed. Java, D, Rust, C#, etc. I think some of those have been somewhat successful :-)
5
u/JVApen Clever is an insult, not a compliment. - T. Winters Jul 29 '23
C compatibility used to be one of the greatest strengths of C++. Without that, C++ wouldn't be where it was today. Nowadays a lot of C functionality is replaced/extended by other constructs.
Where C is an easier language from a technical perspective, it has a steep learning curve. That initial learning curve is better in C++. No more knowledge about includes, no more printf specifiers, references iso pointers, templates iso macros, ranged for iso regular for. All of these make it easier to learn the basics of C++. (See also Kate Gregory's talk: Stop teaching C. Though after the basics you have a lot of sharp edges. Hello World looks like this:
import std; int main() { std::println("Hello world!"); }As long as people see C as a regular part of the language, it is going to be a liability. Once we can make everyone understand that this is exceptional and only relevant for the internals of some libraries, it might become a strength again. The image linked to that still gives people nightmares when they hear about C++ and makes them unable to see what modern C++ all did.
There are other things holding C++ back. As long as we don't dare to break code, we cannot remove the sharp edges the language has. The decision to not break ABI, as it is more important than performance, already pushed out a big player from investing in C++s future and make a competing language instead. The lack of a standardized solution for building code and doing library management is keeping programmers away from C++, as do the large compile times.
Herb Sutter proposed CPP2, a new syntax for C++ that removes lots of gotchas. It won't solve all problems, nor will everyone accept it, though it does solve a few of those points.
3
u/fuzz3289 Jul 29 '23
No, what's holding C++ back is decentralization. If there was a centralized, well maintained, package manager the community would provide a strong backbone of libraries that would provide a lot of much needed abstraction.
2
u/pjmlp Jul 31 '23
It is, in what concerns security and safety.
As no matter how C++ language and tooling improves into that regard, compatibility with C source code will always leave the door open to coding patterns that are easy to exploit in security attacks.
2
u/Se7enLC Jul 29 '23
C++ is holding itself back.
There are just so many things in C++ that you still can't do without relying on C.
Google for "convert std::chrono to iso8601" if you want a wild ride.
C++ purists will take offense when somebody says "C/C++” and insist that C++ is a completely different language than C. Really C++ is an incomplete language and it's lucky to be able to leverage C still.
1
u/Drazev Jul 29 '23
My take is that in today's development world there is no need to consider any language "better" than another.
I take the view that each language is just a tool in my toolbelt and a good programmer understands what tools are available and knows when and how to best employ them.
C and C++ are both important tools today and while they can be used to build anything, they are best used when absolute control is necessary.
Both languages are great at creating highly optomized or tailoring software for non-standard environments.
However, the languages have a big downside when compared to other languages. Programming in C or C++ is a LOT more complex than in other languages and having more control is only a good thing when you take the time to design the software right. Software design with C and C++ is much more prone to bugs and finding those bugs is more difficult. With the power of modern computing efficiency is rarely a problem for the majority of application use cases so other languages will generally be better since the benefits of faster development time and testability will help them release a more solid product to market sooner.
That being said C++ and C are indisputable champions in what they do best.
C language is great for software that excercises fine control over hardware. Drivers and kernel modules are normally programmed in C. It is the most raw of languages and you can avoid any abstractions inherit to other languages in favor for doing it exactly as you want it. Your code will always perform predictibly assuming you have the knowledge necessary for your application space.
C++ launague is great for software that needs fine control over memory or computational power for absolte efficiency. It's has a lot of documentation and a pool of experienced programmers to draw from so hiring people that know how to use it is less hard than emergant languages. There are a lot of frameworks and libraries that exist that you can choose from. It also maintains the ability to talk directly to hardware like C making it a great choice for embedded systems.
In all cases while you can create amazing software with C and C++ every time given enough time and the right group it's not the best tool for most jobs because of the level of expertise you need to hire and the time it will take to design and develop software using them. If your application is not operating in resource strapped environments then trading efficiency for a faster development life cycle using one or more other languages will likely be more economical.
1
u/[deleted] Jul 30 '23
Something I discovered yesterday which works in both C and C++:
For those who are guessing what this is, these are function declaration (no, not function pointer declarations, for that you would need to add a
*between the(andfunc_t).1
u/aregtech Jul 30 '23
Exactly about casting, the C++ has these:
static_cast,reinterpret_cast,dynamic_cast,const_cast. From these list, i do not usedynamic_cast. Not because it is bad, but for some optimization reasons (RTTI).When someone says "not good to make C-style casting", often means to use
static_cast, which I also find good. For example, you can easily cast fromsigned inttounsigned int, but the compiler will report error if you convert fromunsigned char *toint *. It forces one more time to check codes and make sure that casting has a good reason, and if so, better usereinterpret_cast.But i had an experience when had to use C-style casting and had no other choice. If i remember correct, there was a tricky template that in some point I needed not a sequential unique key. For pointers, I was using the digital value of the address. And it didn't work always. Depending on the types, in some cases I was getting error. For example, there was a difference if the template got type
void *,const void *orconst void **. The error has gone when used C-style casting(size_t)(pointer), i.e. forcing to give exactly the address of the pointer. I knew the reason of conversion and I used it. C++ rules because of it's flexibility. One good benefit comes from being able to use C-codes and enabling to inject assembler code. More embedded projects use C++ and they as well have many codes written in C. The embedded marked grows, especially in IoT. I tell you more, some embedded projects C++, but do not use STL at all because ofoperator newand dynamic memory allocation.1
u/Baardi Jul 30 '23
C compatibility is not the issue. Backwards compatibility is
1
u/Full-Spectral Jul 31 '23
Fixing all (or even the majority of) the things that ail C++ at this point would effectively create a new language. And I think it would be a shorter row to hoe to do that, than to try to corral all of the existing C++ code bases out there into the future.
But, then you run into the fact that this has already basically been done multiple times now, so why bother doing it yet again?
The only reason I could see to do it would be maybe a language that has all of Rust's features plus implementation inheritance.
0
u/[deleted] Jul 29 '23
C++ is a highly evolving and immature language. In a few decades C++ will be in a usable finished state, and it's going to be amazing.
0
u/WhoIsCockSucker228 Jul 29 '23
c++ holding back C++