7 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?
- 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.
- nly 3y agoClasses, which have constructors and destructors, simplify code immensely..so do copy and move constructors and operators.
- flohofwoe 3y agoThis would add a lot of complexity into the C compiler and stdlib, and C would just end up as another C++. IMHO it is still important that a C compiler and stdlib can be written in reasonable time by an individual or small team. And just one example (because it's one of my favourite topics where the C++ stdlib really failed): std::string as a standard string type has so many (performance) problems that it is borderline useless. You can't simply use it anywhere without being very aware of the memory management implications (when and where does memory allocation and freeing happen - and std::string does its best to obscure such important details), and this isn't just an esoteric niche problem: https://groups.google.com/a/chromium.org/g/chromium-dev/c/EUqoIz2iFU4/m/kPZ5ZK0K3gEJ https://groups.google.com/a/chromium.org/g/chromium-dev/c/EU...
- 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.
- 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.
- 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 ago
- 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?