I've preferred C to C++ and D to C++ for a long time. To me when I read C++ code it's like the modem disconnected in the middle of the editing session with all the : and << flying around.
For things that don't need classes (and there is a lot of code) I stick to C. But if I need the OOP benifits then I go with D. As a bonus, the C programmers and JavaScript programmers that I work with can read the D code and get most of it on the first try.
If you haven't used D, spend 30 mins and write a four function calculator. Then take the remaining time adding some kind of internal function line sin, cos, etc. You'll be suprised at how fast you can pick it up.
Granted, the above is a toy exercise, it's not writing an enterprise solution. But it's a little more than the standard "Hello Sailor, new in town" program that people start off with.
I also prefer C and D to C++. But used wisely, C++ can be a really nice language after all changes that started with C++11.
The key is to realize C++ is not a language, but a federation of languages. You need to cherry pick a subset you what you want to use and stay disciplined. Otherwise, it is a mess.
The whole "subset" argument is pretty thin. There are style/convention guides, and there are are more obscure features in C++ the same way there are more obscure features in other languages. But it is not necessary to cherry pick a subset.
Out of places that are vaguely keeping up with C++, the only thing I'd consider sub-setting that is commonly applied to C++, is disallowing exceptions and RTTI. There are at least decent reasons for this. Yes, there are places that have additional constraints (like "C with classes" style, i.e. no templates), but it's much more rare (and even more rarely technically justified).
Sub-setting should not be confused with the fact that in many cases, the language does not push you as hard down a specific path (for better or worse), yet it might be beneficial for a specific company in a specific domain to have a common solution for something, which results in the company style guide saying: "for this use case, use company_lib::foo, not XYZ".
Where I work we use pretty much all of C++, as appropriate, that is there is no blanket ban on anything, or official subset. That does not mean that e.g. there is virtual inheritance all over the codebase; that's a feature you should pretty much never need to use. Writing code appropriately and consistently can only ever be done via discussion and code review; no subset will ever magically fix these issues anyway.
My problem when trying to pick up C++ (to write a simple SDL2 game) was that I just couldn't understand the RAII concepts, especially with modern smart pointer classes and STL container classes, and understanding how to use these three features together to be productive. (I just wanted to make a simple grid of cells to port over my simple PICO-8 lemmings-like game.)
Coming from C and thinking of everything in terms of pointers, it was already foreign enough, but the problem was that I couldn't find a definitive reference on how these features interacted and how to use them together successfully. So I gave up on C++. Maybe if I was motivated by needing it for a client project I might have gotten further, who knows.
I suggest you take a serious look at C++ again. It truly is a versatile and powerful language. While it is true that there are a bunch of language features that can be misused, it places the onus squarely on the Designer/Programmer which is how it should be. You can use it as a thin wrapper over C or go as deeply as you like into the OO and Generic programming paradigms. At the same time all abstractions have minimal runtime overhead and pay-as-you-need only.
I was lucky that i discovered C++ early and hence started with it as a "better C". I was not exposed to a lot of upfront complexity (eg. template shenanigans) thus making my learning curve easier. The big mistake people new to C++ make is trying to learn all language features and dark corners. Instead you should look at various aspects of the language separately and understand their applications. That way you learn how to model the problem domain using the appropriate syntactic features of the language. Here are a few different ways of looking at and using the language.
1) As a better C - You can define stricter types, control memory management using techniques like RAII/Smart pointers and enforce better modularization.
2) As an Object-Oriented language - Here you design class hierarchies and provide interfaces, domain libraries and frameworks.
3) As a Generic programming language - Here you define types and learn how to combine them using composition and delegation.
It might be helpful for you to read some of the older C++ books (Modern C++ IMO is more complicated since it mixes the language features in a free manner) to get at the root of "how to think in C++". To that end you might find the following useful;
1) Ruminations on C++ : A Decade of Programming Insight and Experience by Andrew Koenig and Barbara Moo - short chapters explaining various implementation techniques in C++.
2) Scientific and Engineering C++ : An Introduction with Advanced Techniques and Examples by Barton and Nackman - This book will teach you how to design in C++
3) Multi-paradigm design for C++ by James Coplien - Advanced book teaching you how to map problem domain concepts onto language features.
IMO, If you grasp the gist in the above books, you will understand "the heart of C++" and can easily pick up "Modern C++".
The books i had mentioned are old pre-C++11(hence you can easily get cheap used copies) but which give you insight into "how to think and program in C++". They are still relevant, though might not see usage currently, because the language/libraries have evolved.
I am ambivalent on "Modern C++"(they have made it more complicated and invented a new language) and still ramping up on its features and nuances. Haven't really found any insightful book so far (except for "C++ Concurrency in Action" by Anthony Williams which of course is specialized).
However i recently found "Modern C++ Programming Cookbook" by Marius Bancila which seems like a very nice catalog of all the C++11/14/17 features. This seems to go well together with Stroustrup's book.
That's a pretty vague statement though. vector and unique_ptr are class templates (generic classes). To understand them you need at least a basic idea of how that works, which you probably do. After that, you just need to understand their API, which includes copy/move constructors, and destructors, which encompasses RAII. There are tons of blog points explaining RAII.
To be honest, I don't remember exactly what feature it was. Just that I couldn't easily encapsulate a 2d grid of "replaceable" cells. This was like 4-5 months ago and the only documentation we had on C++ at the time was the official Stroustrup book, which was dense.
That sums up my experience. It's hard to find good quality docs, that's up-to-date and not just a beginner's intro that doesn't answer my questions. The only other alternative usually seems to end up being man-pages or other extremely dense and legalesque docs like a ISO standard or something.
Thanks for the sample grid, I've bookmarked it, this'll be helpful for me and my son to understand where we took the wrong road in our implementation.
1. const correctness is awesome. I miss it so much in C# which I happen to code a lot as well. When you have a function which accepts `const Grid<int>& grid`, you can’t call resize() nor change cell values. If you’ll try, the code won’t compile. My given name is Const so I have bias, but still.
2. asserts. They aren’t even C++, these are from C, but IMO they have better ergonomics than C++ exceptions. They compile into nothing in release builds i.e. don’t affect performance, but in debug builds they trap to debugger right away, showing what exactly is not OK. For production code, sometimes it’s a good idea to use preprocessor trickery to turn failed asserts into scary log messages, in release builds.
P.S. It’s possible to implement much better resize(). When neither old nor new size is empty, the version on that gist will essentially turn old data into garbage. A better solution for that case, make a new vector on the stack, write a loop to crop or expand the items (e.g. calling std::copy_n), then call vector::swap to replace Grid::data vector with the newly built one.
> The key is to realize C++ is not a language, but a federation of languages. You need to cherry pick a subset you what you want to use and stay disciplined.
The issue is that no matter how disciplined you are, others will be using a different subset of the features available and you end up using different "languages", even when everyone on your team is ostensibly writing "C++".
Aren't all powerful languages poised to have this problem?
You can write generic imperative C++ by using lots of templates, OO code that looks like Java, low level imperative code similar to C, and even functional code.
I can personally say the same thing about Haskell or Common Lisp. Perhaps it's not that extreme, but there are many ways to solve problems in those languages. Even some funny jokes about Haskell exploit this:
> Aren't all powerful languages poised to have this problem?
Probably. How many of these powerful languages started out that way and received widespread adoption though? C++ was basically C with classes when it become big. C# and Java are complex beasts now but both gained popularity when they were much simpler.
Programming languages need to be comprehensible to your average programmer and not just a few elites.
I feel a bit that way about rust at the moment. I am still learning, but I have found that a lot of libraries seem to use very different interface modalities, so I spend a non trivial portion of my development time figuring out how to glue these disparate things together.
The alternative is using totally different languages, the barriers between which are much higher than between different subsets of C++. You still benefit from being part of the C++ ecosystem.
> The key is to realize C++ is not a language, but a federation of languages. You need to cherry pick a subset you what you want to use and stay disciplined. Otherwise, it is a mess.
I've noticed two strains in language design, which I internally call Scheme-style and Common Lisp-style:
Scheme-style languages are the trimmed-down languages, often built around a big idea which animates the rest of the design but not always; the defining concept of Scheme-style languages is always how much they can remove to get a pure design, a design which supports writing code in one style. Python, Smalltalk, C, and Scheme are all languages in this mold. Java is kind of a compromised Scheme-style language, or two of them (C and Smalltalk) rammed into each other at high speed, which also goes for Objective-C.
Common Lisp-style languages have mini-languages inside of them, or at least support multi-paradigm programming as a core design goal; you can trim a Common Lisp-style language into a Scheme-style subset, but you can't extend a Scheme-style language into something more Common Lisp-like unless you really are dealing with Scheme and have (hygienic) macros to use to add more syntax. C++, Perl, and Common Lisp are all Common Lisp-style; Unix shell is Common Lisp-style in practice because it encourages programmers to write in a bunch of relatively Scheme-style languages at once, all in the same program.
Note that both Scheme and Common Lisp have a conceptual core in the Lambda Calculus and axiom-like special forms. C++ lacks this and Perl doesn't really have it either, but most people don't write huge codebases in Perl.
> Java is kind of a compromised Scheme-style language, or two of them (C and Smalltalk) rammed into each other at high speed, which also goes for Objective-C.
Not much to add to the conversation here, but for some reason every time I read this it gets funnier and funnier.
But I do like your breakdown, it’s what I’ve been thinking whenever I see discussions like this.
Unfortunately, it seems really hard to learn a modern "good" subset of C++. Too many resources teaching it in too many ways. As an outsider, I have no idea what is good or standard.
Even when I only really need C, the contract programming that D provides is really helpful as it provides a macro free way of doing rigorous assertions etc. in a way that I can turn off without infecting the actual logic code.
If a struct's payload cannot be null, for example, getting the compiler to insert checks into all uses in debug builds can be done easily
That and metaprogramming in D is all compile time so you get RSI-free hands, for free.
>> I've preferred C to C++ and D to C++ for a long time. To me when I read C++ code it's like the modem disconnected in the middle of the editing session with all the : and << flying around.
I'm sorry, but even though I think D is a great language, there are plenty of usecases where D isn't a viable alternative to neither C or C++.
If D had all those missing features, or if you compared D to some other mainly-GC:ed, statically typed and compiled language, I think the syntax comparison would have been more fair.
This is one of the things i dislike most about templates in C++. Even today reading template code with its nested syntax takes me time to unpack and understand. Symbols do affect comprehension and i would be quite interested in knowing whether somebody has done a study w.r.t. the various programming languages.
How much software genuinely requires not having a GC? If you don't want it, don't use it, but using it can make your software completely safe and fast (Garbage Collection is often faster than RC as long as pause times are acceptable, and added memory use).
A lot of the standard library ranges are inherently lazy and therefore require no interaction with the GC. Exceptions are also nogc now.
If you can tolerate GC then like 20 languages (as or less obscure than D) enter the conversation. If you can tolerate GC and the performance implications that go with it, then using C or C++ is rarely a good choice to begin with (well, other than legacy, which is a valid and common reason).
D isn't defined by performance or memory management, however. There are many D features which are just blisteringly professional in how they approach software development: Using D is conducive to writing good software.
Metaprogramming, for example, in D is obvious and almost fun. The compile times are ridiculous compared to C++, i.e. I can build the main D compiler in three different configurations in about 5sec on my machine.
I'm not saying that D is defined by that. I'm simply saying that if you're comparing to C or C++, you are talking about use cases that are defined by performance and/or memory management (or else, they are being used for legacy reasons). If you have a green field project that doesn't have massive performance concerns, and doesn't have to be specifically in C or C++ for other reasons (toolchain availability, available developer resources, etc), you probably aren't using C or C++.
Some people would argue that you can use D for equally high performance things to C++, and make sure you use the GC very selectively, etc. However, you don't appear to be making that argument. If you aren't, then there's just no real point comparing C++ and D. If you don't have any of those requirements, and you are ok with obscurity, you have much stiffer competition from many other languages like Haskell, Kotlin, etc.
There are people who specifically use D for writing high performance applications. They find it faster than C++ for a rather subtle reason - D code is more plastic, meaning it is easier to refactor D code trying out different algorithms looking for a faster one.
I'm sure these people exist but they're the exception rather than the rule. The two biggest industries in SG14, the C++ low latency study group, are games and HFT. In both these areas, D penetration is pretty much zero. And in HFT at least while many features would be nice (especially reflection), I can't imagine it would be anything but a performance hit. Just the fact that the best D implementation for performance is llvm based, and most people do not find that llvm produces assembly as good as gcc or icc, is already an instant global hit.
I didn't intend to imply otherwise but I see my wording was unclear. I meant that D generally would be a hit, though it's only really speculation either way.
There is no inherent reason that D code would be slower than C++ code.
I should know, I've written a C++ compiler and know where the bloat is buried, and designed D to eschew features that would be inherently slower than the C++ counterparts.
You can even turn off the runtime array bounds checking.
LLVM and GCC are so close together these days that it makes almost no difference practically, and when it does you should start optimizing based around your code not your compiler.
Dude I mean the context in which I'm discussing this is a large organization operating on timescales shorter than microseconds with numerous people involved in optimizing every single part of the pipeline. We are waaaaaaaaaaay past the point of "premature" optimization. This is just optimization, and optimization where a few percent difference between compilers is huge.
Your comments read like you're explaining optimization to a beginner, it's a bit bad faith tbh.
GCC and LLVM have broadly similar performance characteristics(i.e. for any arch/uBenchmark combo there is another that puts the other one faster).
If GCC is appreciably faster for your purpose, then fair enough but LLVM is not drastically slow by any means (Especially when you consider that any "Anything you can do..." Between LLVM and GCC moves much faster on the LLVM side due to a much saner codebase)
You are talking about it like comparing a Ford to a VW they are pretty much the same, but what the poster above talks about is more like comparing formula 1 cars where a single percent of engine power can win or lose you the race. The difference may be small enough for by far the most purposes, but this one is where the small difference can cost a lot.
HFT doesn't subset C++ nor does it ignore the standard library. Given you're totally wrong about one I'm not inclined to believe you on the other, and I have an examples from the standard library used in game dev eg atomics.
Well you can certainly elect to torture yourself if you really want to. Atomics in C++ compile to assembly in very straightforward ways and there isn't really an enormous design space there. Preshing certainly makes it seem like Ubisoft is using C++ atomics; if it makes sense for them with so many developers and creating AAA games... I'd be curious to know the motivation for writing their own.
Except that in HFT we don't generally disable RTTI or exceptions. I work at a top HFT firm, have friends/colleagues at other top firms, have seen multiple people like Carl Cook at Optiver state in talks that they use exceptions... So what on Earth are you talking about?
Not using some or most of the standard library is precisely not an example of subsetting C++. Just because the C++ standard library has a hash table available doesn't mean that every project has to use it. Companies standardizing their own high performance data structures where it makes sense, and other things as well, is just something that happens and often makes sense independent of language.
Ultimately, though, this is the real tragedy of D. It started as a C clone, and continues to be named like it's a C clone, but it is no longer a C clone. It is a general-purpose memory-aware compiled programming language inspired by C++, but it is pretty different by now! People expecting a C clone miss what else it has to offer.
(Can it be a C replacement? Sure, but so can Rust/Swift/Go/etc)
Just like C does for calling into main(), doing floating point emulation if CPU not available, calling library initializers (common C extension), handling VLA allocations.
Just because it is tiny does not make it inexistent.
Then there is the whole POSIX, which is kind of runtime that wasn't made part of ISO C, but follows it everywhere.
Linux kernel surely used it during the time they had VLAs in, they are now mostly removed thanks to Google efforts reducing memory exploits on the Linux kernel.
One just links another implementation, just like printf() vs printk().
I’ve seen large, poorly written Java programs[0] where an entire core at 100% is dedicated to the GC. I have never seen that behavior for things like ~shared_ptr in C++ or similar languages.
Then again, OOMing due to leaks is something you’re more likely to see in poorly-written systems in non-GC languages (Objective-C, C, C++, etc), so there are certainly trade offs.
[0]: Maybe “poorly written” is unfair. Perhaps “written by people who are unaware of how to write code that performs well in a GC system”.
Probably the most important usage of C are shared libraries. How well it will work if I have D library with it's own GC, another Go library with second GC and app written in C# with yet another GC?
You are right most software it doesn't matter if you have a GC. However then D is not competing against C and C++ it's competing against almost every other language.
If you never allocate from it (which is able to be verified very easily with the @nogc attribute), it will never trigger.
You can disable it and trigger when you feel it is needed.
You can choose to just leak.
The default GC is OK, but not great. However it recently was made multithreaded in the mark phase so it is much faster than it used to be.
There is a fork based (on linux only IIRC) GC, used by several companies for hard realtime systems, that marks and sweeps in a separate process with CoW pages.
Not using the GC is rather easy, there are plenty of mitigation methods.
Not using the D runtime is much more involved.
Not linking with the D runtime is a more hardcore endeavour.
D has a built-in GC by default and doesn't force you to manage memory like Rust. Also, the author mentioned that D is pretty straightforward to anyone with some basic programming knowledge (think C, C++, Java, and C# which fits the majority of coders). Rust is very different from those languages. My main goto language is Python and I can understand D a lot more than Rust which confuses me to no end (disclaimer: I'm a total noob).
Go is probably as easy to read as D if not more and is also GC and fast. It is also very spartan. I bet D has a lot of additional features.
With Rust I always find myself fighting the compiler just like with C, it's very strict about everything, no GC, steep learning curve.
Go is okay but you can't get clever with it. You're productive from the start butit becomes routine quite fast and at a certain point there isn't anything new to learn. Error checking is also quite annoying.
D has the best of both worlds and it also plays nicely with C/C++, something that the author of Zig also understood is a must have in any language that aims to be a better C/C++. In D one is productive from the start and there's always something new to learn. And one learns it as they code, without having to first read Programming in D by Ali Çehreli to make any sense of the language.
I also tried Crystal and liked it but one is more productive in D and Go without any prior knowledge of those languages. I was kind of annoyed by the fact that Crystal removed the for loop, which works perfectly fine in Ruby.
If you fight with the rust compiler the same amount as you do with a C compiler than you're making something wrong in both languages. Don't get me wrong but the rust and C compiler are on the direct opposite of the spectrum – for me. I love how the rust compiler is holding my hand to avoid all the nasty bugs i would introduce into my code.
The overall experience is similar. Rust definitely has better error messages. The difference is that in C once your program actually compliles it can also easily segfault at runtime. With D and Go you just compile the program and it works. It only complains when you're doing something awfully wrong instead of being a grammar nazi all the time. Additionaly, D allows you to enable or disable certain features if you wish to. Want safety? Use @safe and @trusted functions. Want to disable the GC? Use @nogc functions. And the list goes on. One can enable all of these on demand.
Don't get me wrong, I like a language with a Gc by default, my favourite language is OCaml, but I can't claim it to be a drop-in replacement for C projects. Go, Rust (or D) stand a better chance at that.
For things that don't need classes (and there is a lot of code) I stick to C. But if I need the OOP benifits then I go with D. As a bonus, the C programmers and JavaScript programmers that I work with can read the D code and get most of it on the first try.
If you haven't used D, spend 30 mins and write a four function calculator. Then take the remaining time adding some kind of internal function line sin, cos, etc. You'll be suprised at how fast you can pick it up.
Granted, the above is a toy exercise, it's not writing an enterprise solution. But it's a little more than the standard "Hello Sailor, new in town" program that people start off with.