9 ms·
I don't write C / C++ so I'm not too aware of what's going on there, but wouldn't someone that wants those features just switch to C++? Is there any reason to c
by frde 3y ago
I don't write C / C++ so I'm not too aware of what's going on there, but wouldn't someone that wants those features just switch to C++? Is there any reason to change C at this point?
- hbossy 3y agoC and C++ are separate languages and lots of people don't like C++.
- frde 3y agoI guess my question is: If you want `auto`, why put it in C instead of using C++ with no other C++ specific feature besides auto? I get people don't like classes / templates / .. but there isn't any reason one has to use those.
- masklinn 3y ago> I guess my question is: If you want `auto`, why put it in C instead of using C++ with no other C++ specific feature besides auto? Because they're orthogonal, and making function bodies less verbose with no loss of expressivity is nice, without needing to significantly alter the language? Pretty much every C competitor has local type inference, and C actually needs more than most due to the `struct` namespace, typing `struct Foo` everywhere is annoying, and needing to typedef everything to avoid it is ugly. Also C++ is missing convenient C features, like not needing to cast `void*` pointers, and designated initialisers were only added in C++20.
- pmarin 3y agoType inference only make the code harder to read. You ended doing mental compiler work when you could just write the damm type. And the people who say "I Just hover the variable in my IDE" It doesn't work in a terminal with grep, you can't hoved a diff file and not even github do the hover thing. Combine that with the implicit type promotion rules of C. Have Fun.
- masklinn 3y ago> Type inference only make the code harder to read. Nonsense. > Combine that with the implicit type promotio rules of C. Have Fun. This sort of trivial TI does not make that any worse. C is broken, it neither breaks nor unbreaks C.
- hgs3 3y agotypeof and auto are useful for writing type-generic macros.
- pmarin 3y agoYeah but looking how auto has been abused in C++ I don't think it worth it.
- peterfirefly 3y agoC23's auto is not nearly as magic as C++'s auto. You can use it for the simple things but not for the perversely creative abuse scenarios you fear.
- circuit10 3y agoI guess one possible reason is if there’s no C++ compiler for an obscure platform as it would be too much work, but there is an up-to-date C compiler
- klodolph 3y agoYeah, seems like half of the embedded architectures are like this. Well, the C compiler is not quite standards compliant, and often made to an older version of the C spec, but give it time—the C2x standard comes out this year, and it may not benefit people in the embedded space until some years down the road. Second-best time to plant a tree, and all.
- klodolph 3y agoYou end up having to turn a lot of C++ features off in order to get the experience you want in certain environments. In an application running on a modern Windows/Linux/Mac system, it’s no big deal to use those features. Some platforms also just don’t have C++ compilers. Yes, they still exist. You buy some microcontroller, download an IDE from the manufacturer's web site, and you get some version of C with a couple extensions to it. And then there are all the random incompatibilities between C and C++, where C code doesn’t compile as C++, or gives you a different result.
- jcelerier 3y agoWhat's "a lot of features" ? -fno-rtti, -fno-exceptions? > Some platforms also just don’t have C++ compilers It's not like they're going to have C23 compilers either
- klodolph 3y agoC++ has a lot of funny rules when it comes to constructors and initializers. It's easy to accidentally to do something unintended, and end up with code that relies on initialization order.
- flohofwoe 3y ago> It's not like they're going to have C23 compilers either Niche compilers like SDCC (https://sdcc.sourceforge.net/ https://sdcc.sourceforge.net/) are actually keeping track of recent C language improvements quite well.
- lou1306 3y agoMaybe. But by reading the article one does get the impression that GCC devs (and C2X proposal authors) really like C++: the language is mentioned 16 times, and easily half of the features are lifted more or less as-is from there.
- seba_dos1 3y agoAs a C programmer, it just seems like they finally took the minority of ideas added by C++ that were actually good ideas and added them back to C. Aside of `auto` which I'm ambivalent about (I think the only place where it's useful are macros) those all make perfect sense in context of C and I believe the only reason for C++ having them first is that C++ simply evolved faster.
- klodolph 3y agoIt makes a lot of sense for the languages to be harmonized with each other. Differences like noreturn versus [[noreturn]] does nobody any favors. C++ has all these wacky things you can do with constexpr functions, and C is getting a VERY LIMITED version of this that only applies to constants, addressing a long-standing deficiency, where C provides only a way to define named constants as int type (using enum) or using macros, and you really want to be able to define constants as any type you like. The "const" qualifier doesn't do that, you see... it really means a couple different things, but the main one is "read-only", which is not the same as "constant".
- steveklabnik 3y agoOne of the benefits, historically, to both languages is that they share a very large chunk of the language in common. It's therefore in their common interest to try and maintain that common subset wherever possible. The goal here (just to be clear, from my outside perspective) isn't to unify the languages, it's to ensure that stuff that's the same stays roughly the same. If the same code produces two different things, based on the language, that's unfortunate. Code that works in one but doesn't compile in the other is totally fine, of course.
- ori_b 3y agoYes, so why copy paste C++ into C?
- pmarin 3y agoI wish the C Standard Committe stopped smearing all C++ bullshit in to C. Now that many of the C++ people who promoted those features are abandoning the ship. It's what you get when your C compilers are implemented in C++.
- pmorici 3y agoI don’t understand why anyone would use the “auto” variable type thing. In my experience it makes it impossible to read and understand code you aren’t familiar with.
- unwind 3y agoWell, the obvious (?) reason is to type less, and also reduce the risk of doing the wrong thing and using a type that is (subtly) wrong and having values converted which can lead to precision loss. Also it can (in my opinion, brains seems to work differently) lower the cognitive load of a piece of code, by simply reducing the clutter. Sure it can obscure the exact type of things, but I guess that's the trade-off some people are willing to do, at least sometimes. Something like: const auto got = ftell(fp); saves you from having to remember if ftell() returns int, long, long long, size_t, ssize_t, off_t or whatever and in many cases you can still use the value returned by e.g. comparing it to other values and so on without needing to know the exact type. If you want to do I/O (print the number) then you have to know or convert to a known type of course. This was just a quick response off the top of my head, I haven't actually used GCC 13/C2x yet although it would be dreamy to get a chance to port some old project over.
- shrimp_emoji 3y ago> you can still use the value returned by e.g. comparing it to other values and so on without needing to know the exact type. No no no no nonononono. No! Loose typing was a mistake. I think any sober analyst of C and C++ knows that. The languages have been trying to rectify it ever since. But dynamic typing was an even bigger mistake. Perversely, it's one caused by a language not having a compiler that can type check the code, which C does. I want to actually know what my code is doing, thanks. If you want "expressive" programs that are impossible to reason about, just build the whole thing in Python or JS. (And then pull the classic move of breaking out mypy or TypeScript half way in to development, tee hee.) The only time `auto` is acceptable is when used for lambdas or things whose type is already deducable from the initializer, like `auto p = make_unique<Foo>()`.
- rwmj 3y agoC++ drags in a ton of other baggage that a large enough number of programmers don't want.
- deleted 3y ago[deleted]
- trelane 3y agoEspecially when there's Go, Rust, etc. these days. There is so much legacy with C++ the language that it's pretty easy do do stuff subtly wrong if you're not rigidly careful and adhering to a style guide that forces you to only use the safer bits.
- Someone 3y agoSwitching to C++ gets you lots of things that aren’t “C-like” (that, of course, is a vague term that, as this thread shows, people will disagree about, but I think there’s consensus that C++ has many features that aren’t C-like), and may get you subtle bugs because of small incompatibilities between C and C++. For example, sizeof('x') is 1 in C++, but >1 in C because 'x' is a char in C++ and an int in C. https://en.wikipedia.org/wiki/Compatibility_of_C_and_C%2B%2B https://en.wikipedia.org/wiki/Compatibility_of_C_and_C%2B%2B
- nequo 3y agoWhy is sizeof('x') equal to 4 if char letter = 'x'; sizeof(letter) is equal to 1, just like `sizeof(char)`? If `'x'` is represented as an `int` in C, shouldn't `letter` in this example also be represented as an `int`?
- eMSF 3y agoThe type of the expression 'x' is int, not char (in C). The type of an expression consisting of a variable name is the type of the variable (as far as sizeof is concerned).
- tialaramex 3y agoNo. The type of 'x' is int. It so happens on your platform (and most available systems today) sizeof(int) == 4. The type of letter was explicitly char, and sizeof(char) == 1 by definition in C. char letter = 'x'; is a type coercion. That literal is an integer, with the value 120 and then it's coerced to fit in the char type, which is an 8-bit integer of some sort (might be signed, might not, doesn't matter in this case).
- nequo 3y agoDoes this mean that `word` in char *word = "xyz"; is a pointer to an array of four `int`s, `'x'`, `'y'`, `'z'`, and `'\0'`? When I evaluate sizeof(*word) I do get 1 instead of 4, even though `*word` is pointing to `'x'`. Where are the remaining 3 bytes in memory?
- flohofwoe 3y agoMany things that C++ added on top of C aren't actually improvements (I guess the most charitable thing that can be said about C++ is that it identifies and weeds out all the stupid ideas before they can make it into C).
- pjmlp 3y agoAt least WG21 actually acknowledges solving security issues and UB problems are a real problem that needs to be sorted out. Meanwhile, WG14, "not our problem ".
- shrimp_emoji 3y agoTrying to figure out whether it's well-defined to compare two pointers to different objects in memory... ...By reading the C++ standard: "it's well-defined" ...By reading the C standard: *self-referencing Zalgo text quoted by dozens of StackOverflow thread debates where no completely confident conclusion is ever reached, although 3/4 people way smarter than you think it's well-defined and blame observations to the contrary on compiler bugs which they've reported to all the major compiler maintainers with varying reception from said maintainers as to whether they agree that those are actually bugs, forcing you, at the end of the day, to realize that you should really be writing your compiler's C, not aspiring to a universal, platonic ideal of C*
- uecker 3y agoWhy? I do not find the wording in the C standard less clear than the C++ wording (where the result is unspecified for unrelated pointers).
- ho_schi 3y agoA usual programmer doesn’t need most features of C++ but there are many important: Generic-programming with templates, a usable std::string, smart-pointers, references, the howl standard-library (streams, file access, containers, threads). The controversial ones seem to be exceptions and classes. Exceptions affect programming flow, exception safety is very hard and the runtime costs are an issue depending on the environment. Class and inheritance are complicated feature, operator overloading is one of the best stuff I’ve seen. But I can understand why many programmers don’t want handle all the special rules involving classes.