10 ms·
The problem with C according to a C++ developer
- datameta 6y agoThe two languages really should be used as different tools as although there is obviously significant use-case overlap, each excels at its own niche. C++ is quite beyond C with classes. Personally, I think C++ should continue to evolve far away from C (though as of C++20 we now have C99's designated initializers). C is "perfect" for what it is, dare I say it. C++ can incorporate things we've learned from other languages like Rust's memory safety and be better for it. Though to be fully honest I haven't totally worked through the revised memory model of C++20 and it would be great if someone could shed some light on that.
- wahern 6y ago> though as of C++20 we now have C99's designated initializers C++20 only supports a thin imitation of C99's designated initializers. In C99 you can a) mix definition forms (designated and non-designated), b) define members out of order, and c) override earlier definitions. C++20 doesn't support any of these. I've often relied on a and c in initializer macros that provide default, non-0 values that can be overridden (using vararg parameters) by the user, but many might see that as unclean. The lack of b, however, significantly diminishes the value of designated initializers: by reordering struct members you invalidate all uses of designated initializers for that type in C++20, which means they're useless to minimize the burden of refactoring. Also, C++20 doesn't permit designated array initializers, which I've found very useful for arrays that map, e.g., enums to strings.
- misanthropian00 6y agoI will continue to say C/C++. To me they are not distinct enough as languages to truly be considered fully separate and they use the same compilers. The fact that they use the same compiler is to me a devastating argument. No other distinct languages use the same compiler. >A language is also its good practices, patterns, idioms The usual argument, but I disagree. If programmers don't have freedom of expression then fuck programming. We should all be free to write our own programs the way we want and not have to worry about standard practices or cultures.
- sacado2 6y ago> The fact that they use the same compiler is to me a devastating argument. GCC can also compile Ada. Would you say C/C++/Ada is a single language?
- oytis 6y ago> The fact that they use the same compiler is to me a devastating argument. Except they don't.
- pleasecalllater 6y agoyes! freedom! dangling pointers for everybody! But seriously: I wish more programmers had really good background (I'm not talking about academia, but e.g. learning from many books and conference videos). The "good practices" are often a set of ideas like "do it this way, so you won't suffer later". Nothing more. This way you can avoid mistakes made by others. Of course, you can do whatever you like and suffer later. C is C. C++ is C++. If they are the same, well... why do they have different names? Let's call them C/C++/Fortran OK? You can use one tool (you called it "one compiler", while in fact they are different compilers with common core) to rule them all, even in the same program... https://www.cae.tntech.edu/help/programming/mixed_languages https://www.cae.tntech.edu/help/programming/mixed_languages. I'd love to see more programmers naming things with care. This way we would have C, we would have C++. But also we would have MB when talking about megabytes, not mb (millibits, is that even a thing?). If surgeons would have freedom to do things the way they want... in fact they have the freedom, but they rather follow the "good practices" made mainly by the retrospection after someone made a tragic mistake. But if they really would do whatever they want... let's not talk about it for now, OK?
- MaxBarraclough 6y ago> To me they are not distinct enough as languages to truly be considered fully separate There are plenty of subtle differences, precisely because they aren't the same language. [0][1] They're deeply connected, but there are important differences between them, and it doesn't do to gloss over the details. For instance, empty structs are permitted in C++ but might get you into trouble in C. [2] > they use the same compiler Many of the major compiler suites support lots of different languages. You can compile Fortran, C, and C++ code using LLVM, GCC, ICC, or even OpenWatcom. So what? It's true that where you find a C++ compiler, you generally find a C compiler, but the converse does not hold. The TCC C compiler doesn't support C++, for instance. The same goes for many of the more obscure C compilers out there. Also, sometimes a compiler suite will have solid support for C++ but poor support for C. If you want to do modern C++ programming on Windows you might use MSVC, but MSVC has surprisingly awful support for C. [3] > We should all be free to write our own programs the way we want and not have to worry about standard practices or cultures. Disagree. It can improve quality to use strict coding-standards and a strict language. Software development is not about maximising creative freedom, it's about effectively developing high-quality solutions, see [4]. This is the reason software in safety-critical domains like avionics is never free-form, it invariably uses highly restrictive coding-standards like MISRA C, or highly restrictive languages like SPARK Ada. (Hope that wasn't too off-topic, my points there don't pertain whether a 'language' should be understood to include its conventions.) [0] https://en.wikipedia.org/wiki/Compatibility_of_C_and_C%2B%2B https://en.wikipedia.org/wiki/Compatibility_of_C_and_C%2B%2B [1] https://stackoverflow.com/a/21515839/ https://stackoverflow.com/a/21515839/ [2] https://stackoverflow.com/a/755339/ https://stackoverflow.com/a/755339/ [3] https://github.com/MicrosoftDocs/cpp-docs/issues/407 https://github.com/MicrosoftDocs/cpp-docs/issues/407 [4] https://www.fastcompany.com/28121/they-write-right-stuff https://www.fastcompany.com/28121/they-write-right-stuff
- GoblinSlayer 6y ago>C++ has a stronger type system, and unfortunately using C idioms punch a giant hole in that type system, and so C compatibility has a cost in terms of safety. This may sound like C++ has a strong type system, but it's only a little stronger than that of C, if at all.
- hellofunk 6y agoThe C++ type system is vastly stricter than C.
- GoblinSlayer 6y agoDunno, even javascript knows, where an array ends, C++ doesn't.
- hellofunk 6y agoI’m not sure what you’re talking about, to be honest. First, what does this have to do with the type system? Second, C++ typically uses vectors, which know exactly where they end, with the option to use an indexing into them that will throw an exception if you try to access them out of their range. C certainly does not have that.
- alok99 6y agoOf course a C-style array in C++ doesn't have a defined end. It doesn't have an end in C either. But in C++ you don't need to be using those anyway. Part of the issue is that, while you can use the usual C idioms in C++ (e.g. a function takes an array and its length as arguments), you don't need to, especially if you're using C++ correctly. This is one of the points of the article. Like hellofunk said, this has nothing to do with the type system. But the solution to what you're talking about is fundamental C++: use the standard library.
- nybble41 6y ago> But the solution to what you're talking about is fundamental C++: use the standard library. The safety-critical real-time embedded systems where strong static typing would be most useful often don't have the standard library available. Even in C it's not unusual to need to justify every line of code in the final product, which precludes linking against an all-inclusive libc—at best you might get a vetted subset. The design constraints, too, are often incompatible: "no vararg functions" (printf), "no recursion" (qsort/bsearch), "no dynamic memory allocation" (malloc). Where C++ is allowed it is often with additional restrictions like "no RTTI" and "no exceptions" which would rule out large portions of the STL, including most collection types. By the time you take all these coding standard issues into account you generally find that you're writing in a novel language which is just enough like C++ to cause confusion. C++ programming for these environments is not like the C++ which is taught in schools or practiced in other fields. There is a lot to unlearn.
- sassypotato 6y agoC is not a "low-level" language.
- UncleMeat 6y agoDefinitions shift over time. Yes, C targets an abstract machine. Yes, C doesn’t give you direct access to hardware. But in the range of widely used languages C is much “lower level” so it gets that name. Quibbling over the definition doesn’t add to a conversation but just exists to make somebody look smarter than the room.
- Stierlitz 6y agoWhy the problem with C++, it was designed by a committee.
- pjmlp 6y agoLike WG14 has been doing for C since ages.