Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
The Design of C++ (1994) [video] (computerhistory.org)
70 points by pjmlp on Sept 9, 2016 | hide | past | favorite | 55 comments


The language may seem complex to some, but so does math. It is the single most powerful programming tool the world has ever known. Sure, it may be hard to see that it is beautiful, but that is so only because it is a product of both intelligent design and evolution. The language structure is extremely logical, and there is very little that could be changed in it without the danger of destroying its finely tuned fabric built from intricately interacting concepts and mechanisms. In that, it is radically different from the multitude of "pure" programming languages, some also of a respectable age, and it is because of that, unlike those languages, it became, and has been for over a quarter of century, the single most important programming language. Some may say: yes, clearly, this language is borne from good intentions, but are not these the good intentions that pave the road to hell? To that I have only one thing to say: this is where you go looking for good company :)


> there is very little that could be changed in it without the danger of destroying its finely tuned fabric built from intricately interacting concepts and mechanisms

Really? Very little? Finely tuned fabric?

Is diamond inheritance really part of that fabric? And NULL?

C++ is old and has only accumulated, never lost. When I think "finely tuned fabric", I think of Rust, which has the fortune of decades of learning. C++ piled on rvalues and move semantics. Rust is the one that "tuned" its design.

I'm not dissing C++; it's a true veteran, and has witnessed entire languages rise and fall. But let's be honest: the design of C++ is (as one would expect for a decades-old language) organic, not "finely tuned".


Is diamond inheritance really part of that fabric?

Diamond inheritance isn't a problem in C++ as each inheritance path is followed separately. A small price to pay to enjoy the power of multiple inheritance.

And NULL?

NULL is deprecated since C++ 11 in favor of nullptr.

C++ is old and has only accumulated, never lost.

You may want to have a look at the latest standards.


> NULL is deprecated since C++ 11 in favor of nullptr

Pedantically, it's discouraged, not deprecated. NULL is a preprocessor constant defined in so many places, often in contradictory ways. There's no practical way for the C++ standards to deprecate it.

Compiler and analysis tools vendors could (and should) provide warnings when NULL is used when nullptr is more appropriate (which is always IMO).


Actually before nullptr was designed, the good practice in C++ was to use a plain 0, due to the way NULL is sometimes defined and how implicit conversions work.

Seeing C++ codebases using 0 for pointers instead of NULL was already a sign how up to date the respective programmers were with C++'s best practices.


I don't think the usual criticisms of NULL are satisfied by it becoming slightly more type-safe.



References don't allow null.


Rust isn't finely tuned either, it just has memory safety (finely) turned up to 11.

I wouldn't call C++'s design organic, just severly constrained by backwards compatibility. It is finely tuned, but the results are not even close to an ideal that didn't consider C. Finely tuned doesn't imply perfection.


I really like your description, it's pretty much the same as how I think about it. But only when I'm in a subjective 'C++ all the things' mood - which happens often, especially since C++14, there's just so much to love about C++.

When I try to be objective about it though, I'd say you are glorifying too much and skipping on the darker sides of it. You and me probably now exactly how to avoid those and that's also exactly one of the reasons why for us C++ is all utter greatness. But when I see inferior, dangerous, ugly, waiting-for-UB code produced by others I realize it's not all that great. Or maybe still great - after all you could blame their lack of knowledge instead of C++ - but I really wouldn't use terms like finely tuned or extremely logcical. Seriously, I've used the language for like 10 years and it still occurs to me that I see a C++ question and corresponding answer on SO which completely baffle me because I had no clue about that particular implementation detail. There aren't many languages like that. Here's one just from today: http://stackoverflow.com/questions/39422188/difference-betwe... granted I would never write that stuff, but also I really couldn't tell 100% sure how 'const int& x = 4;' at classlevel is supposed to behave.


That is my feeling when I look at enterprise C++ code, which usually never goes beyond "C with Classes" style regardless of the improvements the language has gotten over the years, which is why in the end I came to be an happy JVM/.NET camper that only uses C++ on side projects or when JVM/.NET projects need a bit of outside help.


Let's bring up some technical details to see whether there is a deliberate thought process behind the evolution of C++. Recent versions of C++ have the auto keyword. It's a cool new feature grafted onto C++ from other languages and meant to make the programmer's life easier. It lets you avoid typing things like

  for (std::vector<std::pair<int, double>>::const_iterator it = v.begin(); it != v.end(); ++it) {
    foo(*it);
  }
The above used to be the recommended way to write a loop until C++11, and was supposedly superior to

  for (int i = 0; i < v.size(); ++i) {
    foo(v[i]);
  }
which was considered to be error prone and unsafe. But now things are much nicer and you can write

  for (auto x : v) {
    foo(x);
  }
Looks so much better. Clearly "auto" is a win for C++ programmers. But wait, it turns out that there's actually a bunch of different ways of writing the auto keyword that have subtly different semantics. You can write "auto", but you can also use every possible combination of "auto" with "const", "&" and "&&". So things like "auto&&" or "const auto&" are also perfectly valid and sometimes preferable to simple old "auto".

Let's put aside the fact that you can't explain the difference between "*", "&" and "&&" to a C programmer without them bursting out laughing. Why does auto have six different variants? Isn't this emblematic of how C++ is evolving?


As a light C programmer, no part of this post does not make me WTF hard. Would I actually be expected to type that cataclysm at the top? What's wrong with the C-style loop? Was 'auto' the best name they could come up with for this feature?


They have chosen auto, because it was already a reserved keyword, but it was practically useless.


At least C++ offers me tools to avoid leaking resources or corrupting memory, which cannot be said for C.

The fact that C11 made the security Annex optional, is a sign how much the C committee values writing safe code.


for (auto x : v) {

usually you'd use for(const auto& : v) though


Hey, try compiling this:

    template<class T,class...>class C{C<T*const,T,C>a;C<T,C>b;};C<int>c;
(c) Marc Aldorasi


Even if you dislike C++, or like me, came to loathe it, I highly recommend the book he published in the same year, The Design and Evolution of C++ (https://www.amazon.com/Design-Evolution-C-Bjarne-Stroustrup/...). It's very educational about you go about making a successful language in an existing ecosystem, and even after swearing off the language I don't regret one bit the time I spent reading the book.


After seeing P0145R3 on C++17 refinements to expression order guarantees (and many things like that before it) ... I think I've finally started to actively dislike C++ now as well, after nearly 20 years of using it daily.

They had a golden opportunity to fix it, but even with C++17 and the expression "a(b(), c());", it is still left indeterminate whether b() or c() will be invoked first. Even between calls in the same program! So what if they have side effects? It might be a picosecond faster if the compiler can run c() first to push its argument onto the stack first!

I'm all for the power for C and C++ to optimize to such efficient code, and not consume many resources. But we're a long ways from the PDP-11 days and processor frequencies measured in KHz, and compiler developers are living in some alternate reality where the only thing that matters are benchmarks, and they'll happily undermine the stability and security of our software by doing things like erasing that call to memset() that cleared your private key from memory; or remove that conditional expression entirely because it detected the possibility of an integer overflow, so that means it can do whatever it wants! ... even though the world has been twos-complement for decades now.

Given the very real security concerns and exploits we keep seeing in code ... I don't believe that using languages full of undefined/unspecified behavior is the way to build stable and reliable systems.

Nobody can keep track of the hundreds of UB cases in C/C++. We all do it, and then suddenly a new GCC comes out with a new benchmark optimization, and now our programs are misbehaving.

I'm willing to pay a 5-10% penalty, and give up compatibility with the Motorola 68000 platform, to get well-defined and predictable behavior. Maybe you keep C/C++ for that gaming OS, or that Wall Street trading system. But on my server? I can spare the CPU cycles.

And yet, everything is built on C/C++. Your OS? That Python interpreter? Your web browser's Javascript engine? The .NET runtime? All C/C++ under the hood. We're building on quicksand; when we need a solid foundation.


>They had a golden opportunity to fix it, but even with C++17 and the expression "a(b(), c());", it is still left indeterminate whether b() or c() will be invoked first.

If the order of b() and c() matter, then they shouldn't be in the same statement. That is an abomination. What you little you gain in terseness, you more than make up for in future headaches.


That is not an abomination. Turning this:

    int val = a(b(c(), d()), e(f(), g()));
Into this:

    auto _1 = c();
    auto _2 = d();
    auto _3 = b(_1, _2);
    auto _4 = f();
    auto _5 = g();
    auto _6 = e(_4, _5);
    int val = a(_6);
Is the abomination.

Expecting every programmer to know that a(b(), c()); may call b() first or may call c() first is an abomination. Having a programmer's app work fine on most PCs, and then suddenly having a security vulnerability that takes down my server because a compiler dev decided to exploit this on my system in order to save two clock ticks on a 4GHz CPU is an abomination.

Here's a more tangible use case:

    template<typename... P> void print(P&&...);
    print("The value of: ", binaryObject.getNextToken(), " is ", binaryObject.getNextToken(), "\n");
Looks sensible, until you find out that the compiler decides to switch the parameter ordering and you get the value before the name.

And it's not just me: the point of that PR was because people were doing exactly that with cout << next() << next(); and they clarified the rules so that was permissible. They just ignored function calls, so we are stuck using uglier than sin operator overloading of left-shift for our print statements, if we want predictable argument evaluation. Apparently the 'performance penalty' there is reasonable, so why not for function calls? So now we have to remember that in C++17, cout's ordering is left-to-right, yet print()'s ordering is effectively completely random.


But this is not particularly good imperative style. You want that to be line-based when expressions have side effects. ("one thing after another"):

  auto key = binaryObject.getNextToken();
  auto value = binaryObject.getNextToken();
  print("The value of: ", key, " is ", value, "\n");
Of course if you are calling pure functions you can use nested expressions. And then the order of evaluation does not matter.


I believe the issue is that C++ does not track what are, and are not pure functions, so the ordering optimisation is, in general, unsafe.


> You want that to be line-based when expressions have side effects.

No, I really don't. I don't want three lines instead of one line of code. And I don't want two named variables leaking into my scope. (I could encapsulate the block with {} here, but if print returned a value that I wanted to capture, then I couldn't do that.)


>int val = a(b(c(), d()), e(f(), g()));

I'll stick by my original assertion. If the order of those invocations matter, then they shouldn't be on the same line, regardless of whether the actual behavior is well-defined or not. Just one example of what can go wrong: parameter reordering is often done automatically by tools, or manually by someone who is not intimately familiar with the code, such as six-months-in-the-future you.

Six operations with side effects--especially conflicting side-effects--belong on six lines. Six lines on your screen is not worth the days potentially spent searching for a future bug.


> I'm willing to pay a 5-10% penalty, and give up compatibility with the Motorola 68000 platform, to get well-defined and predictable behavior. Maybe you keep C/C++ for that gaming OS, or that Wall Street trading system. But on my server? I can spare the CPU cycles.

Come over to the land of Rust, where you can keep your high performance memory semantics and safety :).

Seriously, life-long C++ dev here. Been writing C++ for almost 20 years now and I don't feel like there's anything I could do in C++ that I can't in Rust(both performance and functionality-wise).


I dabble with it every time a new release comes out, but I feel it still lacks many features relevant to me and our customers, ability to easily interoperate with COM like .NET languages, C++/CX, Delphi, C++ Builder do, is one of them.


I'm not a big fan of UB either, but keeping the order of argument evaluation indeterminate seems very sensible to me.

Imagine you have a function like f(int, double). If argument evaluation order is fixed, you can no longer exchange the order of arguments: for all you know, someone might be depending on the first argument being evaluated first!


If there's really a performance advantage to argument reordering then you can do it whenever there are less than two arguments that may have side effects. Safe instances would be:

* constants

* regular variables that aren't using the cast constructor for implicit conversion

* member functions marked const

* constexpr functions

You would only need to actually evaluate function calls that may change global state, or that perform in-place assignment (eg f(x += 1, x += 2)).

I will take the performance impact of doing something 'stupid' like calling f(a(), b()) instead of splitting the expression up into three lines any day over potentially introducing a security vulnerability that I don't even know about and that the compiler doesn't warn me about any day of the week.

Performance is not the be-all end-all of the world. We should not make critical applications insecure in order to get our apps to be 0.0001% faster.

> If argument evaluation order is fixed, you can no longer exchange the order of arguments: for all you know, someone might be depending on the first argument being evaluated first!

... I don't understand how moving from arbitrary argument evaluation ordering to fixed argument evaluation ordering can possibly turn any valid code existing today into bad code. Quite the opposite, it has the potential to fix a lot of code. Anything that relied on the arguments being evaluated backward (I don't even know of a compiler that does that ... yet) would have been technically broken per the previous language specs anyway. So I don't know what point you are trying to make here, sorry.


My scenario is:

* There's a function f(int, double), called in ~100 places, written by many different people.

* For some reason I decide to change it to f(double, int). Consistency, or preparing for some other refactoring, whatever.

* I have to track down ~100 occurrences of f and exchange arguments. Time-consuming but no big deal.

* If any code was dependent upon compiler silently evaluating the arguments in a particular way, then the code was broken, and reasonably competent coders don't write too much broken code. (Moreover, such a dependency is 99% likely to be broken by random changes in codes or compiler options, so chances are that I wouldn't encounter too many such bugs.)

In your world, it is just about impossible. If I want to go ahead, I could either change every occurrence into:

    int arg1 = ...;
    double arg2 = ...;
    f(arg2, arg1);
...or pore through every line calling f to see if it's safe.

C++ already has a reputation of being a difficult language to refactor, and your proposal will make it about impossible.


Ah, I understand what you're saying now. I guess we'll have to agree to disagree, then.

I am much more interested in predictability in my code than I am about reordering arguments in mature codebases.

Lots of languages guarantee expression ordering to follow precedence and associativity rules, and I've never heard anyone say that it was a problem to refactor their code as a result.

Whereas I am 100% certain that there are lots of codebases where this is a ticking timebomb with developers unintentionally relying on the order their compiler decides to evaluate expressions in. And it will blow up when, not if, the GCC devs find some micro-optimization and decide to reverse things on them. And that's not just a problem for them, it's a problem for everyone who relies on their code.


I've spent over three decades using languages that guarantee left-to-right evaluation order (Common Lisp and Java), and I have not found this to be a problem in practice.


The standards body move very slowly. More practical approach is to push compilers to add more options to define undefined behavior so that we have safety with the minor cost of performance. We already have -fwrapv, -ftrapv and -fno-strict-aliasing. We could ask for more and a general -fsafe flag.


> We already have -fwrapv, -ftrapv and -fno-strict-aliasing.

You have it in specific compilers, not in the standard.

Not everyone is able to go installing clang and gcc on their work platform, when there are so many compilers to choose from.

https://en.wikipedia.org/wiki/List_of_compilers#C.2B.2B_comp...


I definitely use -fwrapv already. Unfortunately, there is no -fhonor-precedence-associativity switch in GCC or Clang. And I would need it in both to support all the systems I target.

But even moreso, yes!! I would love if we had a -fsafe directive that turned undefined behavior from "do whatever is fastest" into "do whatever is most expected." I would seriously start paying $100 a year to use such a compiler -- not even joking a little.

Unfortunately, as I've said, the compiler devs seem to have lost touch completely with the developers using the language and love playing these nasty games with UB. So I'm not sure how we can go about getting them to implement such a flag. And I just don't have the bandwidth to fork and maintain GCC or Clang to do it myself =(


UBSan is not quite the same thing, but it's quite good.

http://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html


The sort of puppy-dog C++ style on Arduinos - a sort of pedomorphic variation on "'C' with classes", chock full of singletons and the inability of constructors to operate unless they're called in A Certain Place - pretty much works.

What's less attractive is Dogmatic Bikeshedding C++ OO Fundamentalism.


> What's less attractive is Dogmatic Bikeshedding C++ OO Fundamentalism.

Is that a thing? I always saw more of that in the Java world than C++ -- Java's standard library is rife with the singleton factory decorator monstrosities that have come to be associated with OO, C++ and the standard library have always felt more generic-programy. Now egregious use of obscure template meta programming tricks because of perceived (vs measured) performance gains, I've seen some horrors there...


When the design patterns book was written it was all about Smalltalk and C++.

I guess you missed the boat on CORBA, COM and DCOM, fun days...

J2EE architects were mostly former C++ architects that moved into Java land.

Also UWP APIs are actually the second coming of COM as .NET was originally supposed to be.


its actually the full gcc compiler on arduino.... not that they advertise that.


They were pretty upfront about it being gcc/g++ - the "Arduino" part are the built-in things like Serial.


It's a solid book, but I wish there was something newer. It's 20 years old now!


If America were a year it would be out for all of December.


I agree. I only spent very little time with C++, but that book was both highly interesting and very entertaining to read. Highly recommended!


how can you loathe C++ without caming to loathe it? i mean, everyone is bound to hate it, but you have to try it first to be sure...


How can you loathe Krokodil without coming to loathe it? You have to try it first to be sure...


I think it's flaws, only somewhat addressed in recent revisions, are better known now than they were in the mid-'90s when I learned and started using it, plus the industry wide ... obsession? with OO and especially class based OO has subsided. So someone with good taste who reads and trusts what he reads of these details could, yeah, probably not legitimately loathe it, but at least decide without using it that it wasn't for him.

I changed the initial use of loathe in this context to dislike, thanks for pointing that out.


... I like it ...


Just don't assume loathing is linear...


I finally figured how Dr. Stroustrup can always make C++ seem like not only a reasonable thing, but probably the best possible one. He has chosen a number of goals that, while being reasonable and desirable individually, each tend to complicate the realization of all the others. Consequently, for any of the complications of the language, he can pick a couple of goals and show that they make the complication unavoidable.


We will now call this "Stroustrupping": choosing a large number of incompatible goals and using a subset to defend any argument while ignoring the fact this subset conflicts with many of the other goals, such as the subset used to defend the previous argument.


To be clear, I am not suggesting that Stroustrup is being misleading in any way. I think the complexity is inevitable, given the goals, and I came to this view from asking whether there is one thing about C++ that leads to this complexity. Perhaps the closest thing to a single cause was the decision to make C++ a superset of C, which in turn leads to the use of naked pointers, arrays that do not know their dimensions, and to accessing arrays through pointers. Another issue may be separate compilation, which seems to complicate templates and polymorphism.


I believe it's spelled "Stroustrup"


Makes you wonder whether it's possible to infer the characteristics of a programming language from the name of its main inventor, doesn't it?


A nice "interview" with Bjarne from the yealy revue on the center of computer scince on the university of copenhagen: https://youtu.be/TTVCaZVUvC0?t=156




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: