9 ms·
It's a widely spreaded misconception, that C++ is a superset of C. Nope. Modern C has ton of features, which are impossible in C++, from loosening requirements
by Nginx487 6y ago
It's a widely spreaded misconception, that C++ is a superset of C. Nope. Modern C has ton of features, which are impossible in C++, from loosening requirements to implicit type casting (pointers to int) to main() recursion. They are different languages with different niches. I program both in C and C++, and honestly, I don't see modern application of C outside embedded, where C++ compilers implementation is impractical. C++ currently widely used in OS and drivers development, except Linux kernel of course, the only reason of that design approach is personal opinion of Linus Torvalds.
- i-am-curious 6y ago> I don't see modern application of C outside embedded, C is one of the most popular languages for high-performance code. Most famous example would be the Linux kernel but literally anything that needs the best optimizations possible eventually comes down to C.
- ricattierger 6y agoWhy do you say that? Certain things in a language may increase binary size or something else but at the end of the day these languages all go through most likely the same compiler and should produce a roughly equivalent binary.
- hellofunk 6y ago> these languages all go through most likely the same compiler and should produce a roughly equivalent binary That's not really true. A single compiler may support many languages but that does not mean code written in those languages ends up as the same binary.
- bregma 6y agoThey will produce equivalent binaries, but not identical binaries. For example, GCC's C++ front end will produce a different parse tree for the same C code as the C front end, resulting in a slightly different collection of basic blocks and data flow analysis (obviously, for very trivial examples, it will be identical). The result after all the gimplification and and different middle-end passes and rtl transformations you can end up with a surprisingly different set of generated instructions for the same code fed to gcc and g++. Equivalent, but different.
- account42 6y agoNitpick: gcc and g++ are not different frontends but different compiler drivers. They can both invoke the C and C++ frontends (cc1 and cc1plus) and select them based on the file extension unless specified using the -x option. The difference between gcc and g++ is mainly in which libraries are linked by default.
- bregma 6y agoWell, it's true that the different compilers are cc1 and cc1plus, but given most programmers I've run into here in vacuumland have trouble distinguishing between their IDE and their toolchain, expecting them to understand the difference between the actual compiler invoked by the driver depending on a file extension or '-x' switch vs. the different compiler driver that invokes that compiler by default for a given file extension is a bit of a stretch. Even you seem to be confused by the compiler proper (cc1 or cc1plus) and the front end (the part of each compiler that parses the code and builds an internal representation that then gets passed on to the middle end for optimization and the back end for register allocation and instruction generation). The fact is, the front ends of cc1 and cc1plus will interpret the exact same C code differently to the extent that code containing undefined behaviour will have remarkably different results in the generated instructions. The differences between the cc1 and the cc1plus compilers are not limited to what the compiler driver passes to the linker, they have completely different front ends and only share the middle and back ends.
- account42 6y ago> Even you seem to be confused by the compiler proper (cc1 or cc1plus) and the front end (the part of each compiler that parses the code and builds an internal representation that then gets passed on to the middle end for optimization and the back end for register allocation and instruction generation). Sure, the cc1 and cc1plus binaries come with a middle and backend linked in, but what differentiates them is the front end. Not so much for gcc and g++ where both `gcc file.cpp` and `g++ file.cpp` will invoke cc1plus. Not to mention that with LTO cc1 and cc1plus do almost exclusively front end work while everything else happens in lto1 (for both C and C++).
- Nginx487 6y agoI worked under ULL trading platform, they used C++ with heavy templates, without RTTI and exceptions. If things can be done in compile time, it should be done - which is the purpose of templates. Neither our nor neighbor teams (near 800 developers) used plain C. As Bjarne Stroustrup told, there's no place between C++ and machine code for "more low-level language", everything which could be done in C, also could be done in C++ with the same efficiency. Good tendency however, now both C and C++ developers started experimenting with Rust, probably creating unified community and platform.
- harry8 6y agoThe problem is the one you state. You have to use a subset of c++. Nobody agrees what that subset is. You have to be super-vigilant in code review. Features invite their use. It's the lack of features in c that make it attractive. No magic. You want an object or a virtual, code it up if you really mean it is not just a keyword. You take responsibility for all the code running. Every time I've cut a tonne of latency from a trading engine it's that. People relying on library and compiler without taking responsibility for it. STL is great. Except if you're actually performance critical, when you can beat it easily by solving your problem, not someone else's solution for everyone's possible similar problem.
- mehrdadn 6y agoWith the caveat that I am NOT suggesting this justifies choosing C over C++, I just wanted to mention this talk about how "zero-cost abstraction" is an idealism, not necessarily a reality: https://www.youtube.com/watch?v=rHIkrotSwcc&t=17m30s https://www.youtube.com/watch?v=rHIkrotSwcc&t=17m30s That said, I tried to reproduce something similar, and it seems the issue only occurs in my example due to external linkage (adding 'static' fixes it)... but I can't claim this will always resolve the issue: https://gcc.godbolt.org/z/1vbqo3 https://gcc.godbolt.org/z/1vbqo3
- bregma 6y agoThe "zero-cost abstraction" concept is that you don't pay for something you don't use. If you use a smart pointer, you pay for it. If you don't use a smart pointer, it's zero cost. Any argument that smart pointers are not zero cost to the language because they have a cost when you use them is a classic straw man argument.
- pizza234 6y agoWhat's your judgment on the Doom 3 source code? It's something performance critical, but it's in C++; Sanglard's review of the design codebase was very positive.
- clktmr 6y agoJohn Carmack wrote in 2010: > There is some decent code in Doom Classic, but it is all C, and I would prefer to do new game development in (restrained) C++. > I had been harboring some suspicions that our big codebases might benefit from the application of some more of the various �modern� C++ design patterns, despite seeing other large game codebases suffer under them. I have since recanted that suspicion. I think this shows that C++ works better in controlled (i.e. corporate environments), whereas C is often preferred in Open Source.
- hellofunk 6y agoStrange thing to say, to be honest. Many of the most performance-critical industries are heavy on C++, not C. Games, finance, high-frequency trading, where every microsecond is worth huge money -- these are typically C++ shops. The strictness and ability to move things to compile time are features not available in C.
- jstimpfle 6y agoAny examples of using compile time features that make a difference, instead of making code harder to maintian and increasing compile times significantly?
- hellofunk 6y agoYou’re talking about really two different issues here. I don’t think anyone would suggest templates can’t get complicated. But they are certainly the doorway into maximum performance over C.
- jstimpfle 6y agoNo I don't think I'm talking about something else. And I wouldn't submit to the opinion that performance cricitical code needs to be written in C++. For example, Games are heavy in C++ where there are a lot of abstractions. Engine code is mostly C style as far as I can tell. Optimized code is code that exploits specific properties of systems, hence it is significantly less abstracted than higher-level code. The stereotypical std::sort example doesn't count, really. There are many reasons why std::sort vs qsort (which are both generic sorting implementations - i.e. not optimized ones) is not significant in practice, but it's still among the most frequently cited examples - hinting that there might be few real wins (with regards to performance) from all this compile time and templates stuff.
- hellofunk 6y agoHm, I’m not sure where to start with your assessment here. Your comments on sorting don’t really seem relevant, I don’t think you’re referring to the same kind of compile time programming we’ve been discussing. There’s a difference between generic code, and code that runs at compile time, even though both might actually use the C++ feature of templates. C++ templates are way to write code that supports generics, but they are also a way to do a style of programming referred to as “meta-programming“, these are really not the same thing at all. While you can certainly do without generics in the language, as proven by the success of many many languages, perhaps at the inconvenience of some developers, compile time meta programming is an entirely different area that only few languages really support. The high frequency trading firms and the game studios I have partnered with are typically using C++ specifically for those compile time programming features. They make dramatic performance differences in a wide range of highly specialized algorithms.
- rvz 6y agoYet the 'modern web browsers' we're all using are probably written in C++. I'm yet to see a 'modern web browser' that is fully written and only written in C. (C bindings don't count)
- harry8 6y agoOriginal cross-platform Netscape was iirc. Quality died when they went to c++? Probably less influential on their fate than Microsoft.
- jcelerier 6y ago> Quality died when they went to c++? when they went to c++ it became Firefox. do you think we'd have a better firefox today had it stayed in C ?
- harry8 6y ago/When/ they went, which, iirc was Netscape 4 (?). It was horrendous, crashing, slow buggy, awfulness, which the previous version really wasn't. This was when it was all closed and proprietary. Firefox came a long time later. Well after Mozilla opened. No idea if Firefox would be better if they'd stayed in c. What i do know is that c++ is a vastly, vastly better language now through no fault of the standards committee. G++ got good. Then came clang++, valgrind was an amazing leap forward too. Before all that there really was no such thing as cross platform c++.
- deleted 6y ago[deleted]
- gpderetta 6y agopedantically C++ is not a superset of C, but they share a large subset that happens to be closer to the whole of C than the whole of C++.
- pjc50 6y agoAre any of the C features that aren't present in C++ actually useful for something that doesn't have a better C++ alternative? I can't see main recursion as being particularly important (and I suspect a lot of compilers will let you do it even if the standard doesn't).
- flohofwoe 6y agoC99 designated initialization is the big one. It almost feels like a new language and makes C++ look a bit silly with all its different initialization features. C++20 got a subset of C99 designated init, but it is so much restricted that it's practically useless for anything but very simple structs.
- jcelerier 6y ago> but it is so much restricted that it's nearly useless. that's a pretty harsh take on it - how would you design it considering that initialization order has to be respected (else a lot of use cases would break; it's definitely not uncommon to have a member of a struct to depend on a previous member being initialized) ? I have yet to find a particularly blocking use of the feature when test-driving it - it's on the other hand a great boon for readability (and definitely puts a nail in the coffin of named function parameters proposals imho).
- flohofwoe 6y agoThe ordering requirement is indeed the biggest problem once you have structs with more than a handful of items (maybe one day Intellisense will help with this problem), but the missing array access and not being able to "chain" designators (.a.b.c = ...) are also painful omissions. Clang has demonstrated long before C++20 that it is possible to use the full C99 designated-init feature set in C++, so I really don't understand why the C++ Committee had to cut down the feature when there was already a fully working implementation. PS: as an example for where C++20 designated initialization breaks down, see for instance [1], I tried to replicate this code in C++20, but quickly gave up (with the main problem being that it is impossible to remember the correct declaration order in big "option bag" structs like this. [1] https://github.com/floooh/sokol-samples/blob/e0a93a21b17a6633574fcb8a5eba266e96b9d460/sapp/cube-sapp.c#L82-L100 https://github.com/floooh/sokol-samples/blob/e0a93a21b17a663...
- flohofwoe 6y agoThere is a ton of useful libraries written in C [1] [2], and I feel like there's a little revival in the last few years for using C to write "bedrock libraries", because such libraries are often easier to integrate into "foreign language" projects than C++ libraries. Those libraries also often end up in C++ projects because they are often easier to integrate into C++ projects than actual C++ libraries. [1] https://github.com/floooh/bookmarks https://github.com/floooh/bookmarks [2] https://github.com/nothings/single_file_libs https://github.com/nothings/single_file_libs (here, C/C++ usually means "written in the common subset of C and C++")
- rmind 6y agoLinux, BSDs and all other UNIX-like systems, PostgreSQL, Redis, Nginx, HAProxy, interpreters of Python and many other languages, tons of libraries, ... and the list could go on and go on. A lot of very successful and robust software is written in C. The preference for the C language is certainly not due to a personal opinion of only one person.
- quelsolaar 6y agoCall of duty. Its all C.
- fanf2 6y agoC does not have implicit casting from pointer to int: that is a constraint violation which requires a diagnostic from the compiler.
- turndown 6y agoI had never even thought of main recursion before, so I tested it out. It worked fine in both C and C++; am I misunderstanding something?