8 ms·
Ways to Avoid Complexity in Modern C++ [pdf]
- grabcocque 11y agoC interop. The cause of, and solution to, all of C++'s problems.
- warmwaffles 11y agoExplain?
- jcl 11y agoPresumably, C interop is the cause of C++'s problems because it was the impetus for carrying forward all of C's warts into C++. And it is the solution because it allows you to put C++ code behind a C-compatible interface so that you can use it from a language that isn't C++.
- pjmlp 11y agoThe "Design and Evolution of C++" is a very nice book from Bjarne explaining his compromises regarding what eventually became C++98.
- armitron 11y agoDon't use it? Nothing can salvage C++ at this point. It needs to be EOLed and put to rest.
- vvanders 11y agoWhen you give me a language that I can specify explicit memory layout and support for generics we can talk. Otherwise C++ is going to continue filling the space for large, performance sensitive applications.
- pif 11y agoYes, support for generics. _Full_ support for generics. And RAII!
- chncdcksn 11y agoDoes Rust fit into those specifications? I understand library support is probably not quite there yet, but I was under the impression it could fill the role of C/C++.
- vvanders 11y agoThey get close but I think there's a few restrictions(rightly so from a safety perspective) that keep untyped arena allocators and in-place loading from working(see FlatBuffers for a fantastic library for in-place loading). Edit(since I can't reply yet to child): By in-place loading I don't mean placement new(which untyped arena covers). Usually in-place loading means constructing an object(usually just by reinterpret_cast<T*>(data)) over a block of memory read in from disk/network such that all data members "line up" with data as it was serialized. This also includes the ability to mutate the in-place data but have initialized to some sane values. 99% of the time this is paired with either some sort of preprocessor(usually written in the same compiler as the reader to guarantee data matching). Quite a few games use this technique to allocate levels/fixed entities such that once you've pulled them disk you just need to do some trivial pointer fixup and away you go. There's some pretty incredible speed increases(100-200x) but obviously it's incredibly fragile and complex to get right. Flatbuffers does a good job of trading off fragility for usability by encoding offsets in the placement data. Reading data is slightly slower due to needing to calculate the offset into the data but on the plus side it doesn't depend on compiler specific layouts :).
- steveklabnik 11y agoBy "in-place loading" do you mean placement new? And while Rust does have restrictions for safety, unsafe should give you the power to do anything equivalent C or C++ can do. Of course, many idioms are different, especially for safe Rust.
- 11y ago
- ska 11y agoIn favor of what? There are lots of things it would be crazy to write in c++ in 2015 ... on the other hand there are some things that really don't have compelling alternatives.
- vvanders 11y agoI feel like Rust is getting there but AFAIK there isn't support for an untyped arena allocator(just homogeneous arena allocator) which you need if you don't want to be playing around with raw byte arrays to do Data Oriented Design. I think that they'll get there but it's going to take a bit. Also, I'm not aware of anything that has a wider set of compile targets(all the way to some larger uC up to web via emscripten).
- blinkingled 11y agoHave an up vote for a completely fair point. They keep adding stuff to C++ when the language is already ungodly complex and ambiguous enough. Just stick with C if you need low level control.
- CountSessine 11y agoI don't completely disagree with you, but in practice it's really hard to go back to C sometimes - there are a lot of tasks that are painful and error-prone to write in C. Complicated initialization and teardown logic that can fail mid-way is much easier to write incorrectly without RAII and scoped initialization objects, for example.
- AnimalMuppet 11y agoBut see, I only need low-level control for parts of a project. For the rest of it, it's really nice to have the STL, and the move operators, polymorphism, and and and...
- emcq 11y agoA little confused about the authors decision to do operator overloading with the pipe operator rather than a function chaining interface for the functional bit. Wouldn't a function chaining interface be simpler and use less magic?
- vvanders 11y agoI think it approximates the pipe("|>") operator that you see in quite a few functional languages.
- restalis 11y agoAnd it is a poor choice to consider that in C++, because in here there already is a default non-compatible interpretation for their expression.
- CyberDildonics 11y agoIt depends on the context. The pipe in a data flow sense will be working on data structures, a bitwise or will need to work on two plain data types.
- restalis 11y agoYes, this was what I thought too. I understand where this "piping" of functions comes from and that it deemed to be a readability improvement (for *nix users at least), but it is unnecessary because like you've mentioned there already is a way of doing that in C++ and because at least for me the notation is a little bit confusing (as I'm inclined to read it as bitwise "OR" on function's addresses) and requires me to rewire my brain in a counter-C++ way!
- kbenson 11y agoDid you miss the part that said "[SYNTAX IS OPTIONAL !] Exact operator choice is up to You" ? Why criticize the author about a point they went out of their way to tell you was just a stand in for whatever syntax thought made a better choice?
- 11y ago
- PythonicAlpha 11y agoI used to program C++ a lot, several years ago. My way, today, to avoid complexity in C++: Avoid C++! C++ is at least three languages and the complexity is at least that of three languages. If you really want to be faster at programming and don't need to have the last 5% of performance, do you a favor and use an other language.
- unabashedturtle 11y agoFor some of us, that 5% is incredibly valuable. Really, any HPC application is going to either be written in C/C++ or Fortran. It goes without saying that modern fortran is a hideous language (even though it's still better than Fortran 77). C is nice and simple but without things like namespaces and classes you're going to end up with huge function names. Not to mention a high risk of memory leaks. C++ was awful years ago. Honestly C++11 allows you to write fairly nice code. By no means a guarantee you'll write clean-ish code, but it definitely gives you the tools to do so. Plus modern C++ compilers produce really fast code (that leverages SIMD/vectorization, etc.). I'm a huge fan of Go and Rust but the former is often dogmatically minimalist to the point of not including things like templates, operator overloading, etc. Not to mention garbage collection/runtime which is a big no-no for numerical computing use. The latter is still not mature enough to fully commit to (and is nowhere near the speed of optimized C/C++).
- steveklabnik 11y ago> (and is nowhere near the speed of optimized C/C++). Please file bugs! We consider them as such.
- halayli 11y agoThe advise is pretty useless. Complexity often comes from incorrect abstractions, not from language features. Programmers can build very complicated systems using the simplest languages available.
- mreiland 11y agoI agree, but I think "incorrect" is the wrong term, "suboptimal for the problem" would be a better way to phrase it.
- sklogic 11y agoA language, and especially a low level language, often jumps in front of your nice abstractions and obstructs them with all the unworthy low level details and its exposed bare guts. Techniques for eliminating this leaky abstraction are important indeed.
- xigency 11y agoHm, this seems to be more advice on how to use C++14 as if it were its own language, or as if it were Java, or as if it were Scheme. One problem I have with the advice to use the newest language features in C++ is that it sacrifices one of the biggest advantages to writing code in C++: the ability to compile for multiple platforms. It's extremely unlikely that one has a compiler for every platform that supports all of these features. Additionally, each of these language features adds a small bit of convenience to the developer, while making the compiler substantially more complicated (and hence, more prone to errors). To summarize the points made in the first 10 slides, which is far as I read (sorry)- 1) Always use auto. 2) Use for-each. 3) Don't use new or delete. 4) Use STL for everything. 5) Use smart pointers.
- Arelius 11y ago> it sacrifices one of the biggest advantages to writing code in C++: the ability to compile for multiple platforms. Is that really one of the biggest advantages to C++? Both of the other languages that you mention, can target a huge amount of platforms. Java, Scheme. In fact, I think that's a feature of very many modern languages, And specifically of C, which may be a much better choice if compatibility is your preference. It seems that the choice to use C++ is not likely motivated primary by platform support for most developers.
- deleted 11y ago[deleted]
- pjmlp 11y ago> biggest advantages to writing code in C++: the ability to compile for multiple platforms. It's extremely unlikely that one has a compiler for every platform that supports all of these features. Do you have war wounds from the C++ ARM or K&R C days? I have. That was one of the reasons many of us enjoyed using Java. It was much easier than dealing with all compiler and OS compatibility issues. The situation with C++14 compatibility is way better than on those days.
- hermanhermitage 11y ago
- hawski 11y agoFor some time I think of C++ not as a programming language, but as a make-your-own-programming-language-kit. It's because practically there is no the C++. Everyone uses different subset or even coding style. C++ ABI is not as interoperable as C ABI (if you can even say that it exists). Almost every bigger library reimplements Object - the base interface of everything. Then they need wrappers and adapters between those different object models. Qt for example is not written in C++ - it has to be compiled with their MOC to add signals/slots and introspection around other things. But all this ad-hoc languages suck in regard of tools. Their error messages are unintelligible and everything relies on convensions (hopefully enforced at least by code reviews). I could paste here quote about ad-hoc reimplementations of Lisp... C++ is still necessary (evil) in some cases, but as a C++ programmer I have enough and I plan to move to other languages.
- seivan 11y agoYup, I've had the same experience. Every C++ project or framework pretty much has its own style and I don't just mean code structure.