2 ms·
I write in C only when I need to; in my case I have a limited runtime environment available, so I can't use some other language (which I might otherwise prefer)
by teddyh 7y ago
I write in C only when I need to; in my case I have a limited runtime environment available, so I can't use some other language (which I might otherwise prefer) with a larger runtime requirement, and I also already know C, but not C++. Also, none of the libraries I have to use have C++ APIs. Had I already known C++, and if there were C++ APIs available for the libraries I need, then sure, I might have used C++, but as it is, the results (a C++ program but full of C-style programming because of the APIs) would not be worth the effort of learning C++. C++ also imposes a cost; that of reducing possible contributors to those people who already know C++ – knowledge of C is simply more common than C++.
- comex 7y agoYour other points are valid, but you can use C++ without any more of a runtime than C needs. The main feature that needs runtime support is exceptions, but it’s common enough to use C++ with exceptions disabled even outside embedded. There’s also global constructors, if you’re not already using them from C with GNU extensions; those can be disabled too if needed, but it’s easy enough to make sure the constructors get invoked (most crt0s should do this out of the box).
- teddyh 7y agoYes, the limited runtime is what is prohibiting me from using, say, Python. Runtime-wise, C++ would be fine. It’s the other reasons I listed which makes me not use C++ in this case.
- pietroglyph 7y agoIs C++ -> C interop your only reason for avoiding C++? If it is then you might reconsider C++, because calling C code from C++ is usually as easy as wrapping the #include in an extern "C" block.
- teddyh 7y agoI know it’s easy to call C functions from C++. That’s not the issue. The issue is that since all the API functions are in C, the code of my program will mold itself around those APIs, and my program will therefore have a C “shape”, so to speak. And the small pieces that remain C++-shaped might not be large enough or provide enough architectural benefit for the drawbacks of C++ to be worth it.
- jart 7y agoLearning C++ is easy. What's hard is being able to afford a computer that can compile it in a reasonable amount of time. I've seen projects written in C / Assembly that build over 9,000 objects in about 20 seconds on just one PC, whereas more than a few C++ codebases I've seen in the past couple years, that can be about as long as it takes to build just one file. I honestly suspect the only folks really writing C++ consistently these days, on things like LLVM, are the ones who work at huge companies, with literal supercomputers devoted to compiling their code, which would prevent them from ever encountering the latency issues that exclude poor folk such as myself from any hope of ever hacking on their codebase. It's sort of like that scene of Agent Smith talking to Neo in the interrogation room: "What good is the source code... if you're unable to build?"
- kvakil 7y agoMaybe take a look at gg [0]? It seems to solve the problem of slow compilation quite nicely. [0] https://github.com/StanfordSNR/gg https://github.com/StanfordSNR/gg
- TazeTSchnitzel 7y agoWhy should I have to outsource compilation to Someone Else's Computer?
- pjmlp 7y agoVisual C++ with pre-compiled headers, incremental compilation and linking turned on, does a pretty good job. https://devblogs.microsoft.com/cppblog/improved-linker-fundamentals-in-visual-studio-2019/ https://devblogs.microsoft.com/cppblog/improved-linker-funda... Another tip, use binary libraries and don't build everything from scratch. Modularize the application into their own libraries as well. Speaking of modules, they are part of C++20, with VC++ and clang adapting their implementations, gcc ramping up its implementation, and everyone else that cares about ISO C++20 also ramping up theirs. Fun fact, Swift and Rust codebases aren't that fast to compile when compared against C++ code that isn't doing any crazy template metaprogramming.
- shaklee3 7y ago
- std_throwaway 7y agoC++ is perfectly able to interface and interact with C libraries. Also it doesn't necessarily produce larger or slower binaries. Virtually everything is optional. The only hard part about C++ is to identify the good parts (those that are useful for your project) and to not use the other parts. Then you can do "C with templates" or "C with simple classes that have proper RAII" but you don't need exceptions. The argument that I keep hearing is kind of the "slippery slope" that if one allows a little bit of C++ the rest will come creeping and with it will come all complexity. This shows that project management is weak and tries to deal with this weakness by selecting tools with limited capability. A method that seems to work reasonably well in cases like the Linux kernel.
- hyperman1 7y agoUnfortunately, for any human activity at scale, project management is weak. You can get good quality with small teams, but when more than 100 people are together, you can be sure that someone is going to commit every athrocity they can get away with. And then major damage happens, even if all the other people do aperfect job. See Boeing recently. See chernobyl. See any major disaster. So having tooling that limit your ability to do damage, even at the cost of pain for power users, is one of the few ways to keep humanity in check.
- teddyh 7y agoUnfortunately, in order to pick the good parts of C++ which are appropriate for your project, you first have to know all of C++.