Universal references in C++11 – Scott Meyers

C++11 is a wonderful upgrade for C++, and it has all the things that an upgrade should have. The old things work the same or better, the new things make your life way easier and  productivity should jump through the roof with stuff like auto, foreach or lambdas.

But, as any extension, it adds to the already complex language some more complex constructs. One of them is universal reference, and Scott Meyers, one of the big contributors to the new C++ standard, has a presentation about this complicated topic.

The presentation requires an HTML5 able browser, but if you don’t live in the stone age, your browser should work.

PS: ‘Universal references’ is a name defined by Scott Meyers; the presentation is fascinating. 🙂

Comments

Universal references in C++11 – Scott Meyers — 8 Comments

  1. C++ is becoming as user friendly as cuneiform writing.

    I do like auto and the memory model though 😉

    • It doesn’t have to be user-friendly. Only the programs that you create with them.
      I see that user-friendliness in programming generates idiots. I’d rather have complicated ways 🙂

    • Well, I completely agree that having languages that considerably lower the threshold for entrance (e.g., php) are a really bad idea. There’s enough idiots programming already.

      But that doesn’t mean C++ needs to be this complicated. If one can’t explain an invariant in a paragraph or so (and the universal references can’t be explained that way), it’ll lead to very obscure bugs, and it’ll get closer to a “write-only” language like Perl.

      I for one think that the original C++ references were a really bad idea to begin with. To date I haven’t gotten a reasonable answer as to how they improve programming. The Google C++ style guide, which always aimed to make C++ programming sane by restricting the nasty features, says that one should only use const references, for passing links to objects that aren’t meant to be modified.

      BTW, references are not supposed to be NULL. But in practice they can easily be. Just convert a A* pointer which was originally assigned to NULL. Ideally you’d get a segfault exactly when you do that. But you don’t :). The internal representation is still a pointer, so if you derefence a pointer into a reference, nothing really happens then.

      I see the universal references as an attempt to make references more useful, but it really feels like a patch on a patch.

      Personally I think D is getting better tradeoffs, all-in-all. It’s references are real references. But it has relatively little industry traction.

    • First of all, the universal references actually are not meant to be used by the programmer directly, but by the library writer. Usually you don’t write your own data structures unless, well, you write your own data structures. But I digress.
      The strength of C++ is the basis of C. A lot of decisions made during the first definitions of the language have now to be supported; we can’t deprecate language features. This is why C++11 is such a huge thing: it makes the programmers more productive without sacrificing backwards compatibility.
      It is complicated, but that’s because at times, especially when it comes to templates, C++ was ahead of its time. People like Andrei Alexandrescu followed up with how many things you can do with the template system as it was defined; now, an already masochistic system was improved and extended. There’s no elegant way to do that.
      C#, which in my eyes stands as an amazing ‘well done C++’ (far more elegant than Java), learnt a lot from C++ design mistakes, as well as from Java restrictions. I don’t know D, but C# as an expression form I really liked.
      C++ is well known for allowing you to create incredibly complicated ways to shoot yourself in the foot. But modern C++, with C++11 as an extension, is a productive language and, although quirky at times, it grows in elegance. Sure, you can still fsck things up like in the good old days, but you can choose not to.
      I like the rules from the Google C++ style guide. It’s sane; and I always saw that const correctness solves some quite difficult problems.
      For me, C++ is a fun language. And it’s the first mid-level language (together with C) that made me understand how stuff works at a greater scale. Of course, my first love was assembly language, but that is too much nowadays 🙂

    • I definitely agree with many of the points you’re making. However, I think that C++ often got features without much thinking. Add feature, think later, essentially.

      You say that the template system was ahead of its time. I see it as one of the features that wasn’t fully thought out. They most certainly didn’t intend for the template system to be Turing-complete. That became obvious later. Ah, the joys of writing infinite loops in templates ;). Sure, you can do fun stuff in it. Is the trade-off generally positive? That’s harder to debate.

      And btw, there are better ways of doing static polymorphism than C++. See ocaml for instance (not that I’m a big fan of functional languages – I just think that its typing system was actually designed, well thought-out, rather than incrementally developed with a “let’s see what sticks” attitude).

      Sure, the separation of “library writers” versus “normal code writers” is fine for most parts. When you debug obscure bugs however, it’s a lot more difficult to maintain the separation. Java/C# do a lot better as their checking is far more aggressive. And yeah, when you debug obscure bugs assembly knowledge is very much helpful 🙂

      Actually, I’m curious – what’s the relative runtime speed of Java vs C# these days? (Not startup).

    • Templates definitely went out of hand, but in C++11 there was more thought put into extensions, although some decisions look a bit arbitrary (including the reference collapsing).
      Regarding the Java vs. C#, I have made some tests (it was actually Java vs. C# vs. C). Mono gives disastrous results (3-4 times slower than JRE) but Microsoft’s own implementation might be a lot better (and for my specific example it ran head to head with Java and C++, with small timing differences). But benchmarking languages can get you to obtain whatever result you like. I remember my discussions with now-a-googler-still-(-I-think-), in which he said that Java kicks C++’s arse. And it did in his tests (but he compared an optimized JRE with Borland’s Turbo C++ compiler). Again, as I said, if one wants to make a benchmark and put any language on top (php included), they can.

    • Nope, there’s no option to support C# in Linux other than mono. Perhaps some wine tricks could help, but I don’t think that’s enough or useful in the long run.
      C# and Microsoft’s tools go hand in hand. You can’t really separate the two, there’s no reason why Microsoft would ever write an optimized .NET VM for Linux. They want to sell those Windows Server solutions. I don’t think there is anyone out there who can afford to invest in optimizing Mono (or something similar) as much as Oracle invests in Java, Microsoft in their own .NET stack, or Google in Dalvik. But still, it’s a really fun language, and an interesting platform.
      But now even Microsoft has second thoughts about byte-code/high-level languages. WinRT is more welcoming for native code, and Herb Sutter is one of the biggest drivers of the standardization comittee of C++.