The Raw and The Cooked: C++0x Extensible Literals

Posted by Christopher Smith Tue, 03 Jun 2008 00:43:00 GMT

When I look at C++, and squint a certain way, it appears to be a heroic attempt to retrofit a real type system on top of C’s terribly weak one. C’s weak typing is, for the most part (because we can’t possibly break backwards compatibility… except for when we do), augmented with C++’s strong typing. C’s typeless preprocessor is augmented with C++’s so-thoughtful-about-types-it-is-sentient template system. C’s structs are augmented with C++ objects and operator overloading. C’s weak typing and don’t-ask-me-why-just-cast-it operator are augmented with the far stricter, precise, and admittedly verbose static_cast<>, dynamic_cast<>, const_cast<>, reinterpret_cast<>. C’s unfortunate format string and varargs oriented I/O functions are augmented with C++’s strongly typed std::iostreams. Retrofits can sometimes be stronger and more powerful than other approaches, but they are almost inevitably more complex, less elegant, and generally less lovable than “from scratch” solutions. I think many programmers feel the C++ committee was engaged in an academic exercise to demonstrate just how true this principle could be. Looking over the C++0x proposals, it appears as though a strong sentiment on the C++ committee’s part, was that C++98 was too limited a case study and they could go further to produce more spectacular results, an opinion that has been greeted by jeers and cheers (seems like mostly jeers on the blogosphere, but then… it’s the blogosphere).

One of the benefits of all this effort is that in C++ user defined types are practically first class citizens in C++’s type system, largely indistinguishable from primitive types (Java has also realized they missed this boat and is attempting to correct it, albeit through an alternative trajectory more consistent with its nature with things like autoboxing). I say “largely”, because there are some subtle differences (well, in true C++ fashion, they are only subtle until you encounter them, at which point they are as subtle as a punch in the face) that continue to annoy, and the C++0x committee’s holy quest is leading them to find new ways to address this. Perhaps one of the more interesting efforts to bridge the remaining gaps between user-defined types and primitive types in C++ is the Extensible Literals proposal.

Comments

  1. Glen about 1 month later:

    Nice article. Funny too ;) If you think the proposals have short comings and need polish (and I’m not one to argue as I don’t know), why not suggest a few of the short comings and some ideas on how to fix and polish them in your next article. :)

Leave a comment