3 ms·
The article mentions C++ three times, all in contexts equating it with C. But C++, as it is coded today, is a very different language from C, and does not suffe
by ncmncm 6y ago
The article mentions C++ three times, all in contexts equating it with C. But C++, as it is coded today, is a very different language from C, and does not suffer from the problems that make C an extremely poor choice for starting any new project that might matter.
All the article's arguments for C apply substantially moreso to C++. Thus, the article leaves us with no objectively plausible reason ever to code in C, except where artificial constraints mandate it, or where merit doesn't matter. (I leave to the reader to decide where Linux and BSD kernels fit in that.)
There is especially no excuse for systemd to be coded in C.
- coliveira 6y agoI disagree. First of all, most C code can be run as C++, so everything bad you can say about C applies to C++ as well. Second, C++ introduces its own problems that also make programming unsafe compared to other managed languages and even compared to C.
- qznc 6y agoYou would constraint yourself to a subset of C++ which is not C. For example, forbid all raw pointers and only use smart pointers. Now your memory is freed automatically via RAII and use-after-free errors are much harder to create.
- coliveira 6y agoAs you said, this is a subset of C++. But everyone debates what the right subset should be. In reality C++ is incapable of solving the problem, it can only propose good practices.
- aromatic_dev 6y agoThough if one only uses smart pointers they will have to accept the performance hit that that brings -- in high performance situations, this may be unacceptable.
- ncmncm 6y agoCircumstances where using smart pointers affects performance are vanishingly rare. Where performance matters, on any critical path no allocation or deallocation is done, so a smart pointer can have no effect. On other paths, any difference is swamped by overhead in malloc and free. So, in actual practice, no.
- wheybags 6y agoThere's some argument to be made about bookkeeping and (maybe? I'm not sure) an extra level of indirection with shared_ptr, but the cost of unique_ptr is literally zero.
- ncmncm 6y agoPassing unique_ptr by move works out to be surprisingly expensive, so one doesn't, where that matters. (It is usually a mistake to have unique_ptr visible in an interface at all, but passing a pimpl type, implemented with unique_ptr, by move costs exactly the same.) And of course no one who cares for their reputation is seen using shared_ptr.
- boring_twenties 6y agoWhat's the expense of moving unique_ptr -- is it resetting the source to nullptr?
- mawww 6y agoMy guess is that because in most common x86_64 calling conventions the caller is responsible for destructing the parameters, it has to run the unique_ptr parameter destructor which needs to check if it was moved from in the callee or not. Additionally, because this destructor is not a trivial one (i.e. it does something), unique_ptr cannot be passed directly through a register but must be spilled on the stack.
- 6y ago
- qznc 6y agoC++ still has a lot of rope to hang yourself. For example, there is the stuff Rusts borrow checker prevents. Or that members are initialized by declaration order and a wrong initializer list can lead to undefined behavior. Or all those implicit conversion rules (ok, the annoying tendency to convert to int is inherited from C). The most ugly aspect is that C++ has a lock-in effect like Whatsapp. As long as your codebase is in C, you can easily interface with nearly all other languages. Once important parts are in C++ though, only C++ can reasonably interface with it.
- ncmncm 6y agoIf "lock-in" is something wrong with C++, it is equally so with Rust. But in fact anything you want callable from C, in either C++ or Rust, is easy to keep that way. To the other point, you can write bad code in any language, Rust included. But you don't have to. A language can help by making good code easier to write than bad code. C fails so frequently by making good code much harder to write, instead.
- sweeneyrod 6y agoC++ is about 17 languages