Home > C++, uml > whatIsThisFor


Check out this C++ code fragment at the beginning of  the MessageBase class template definition:

After staring at it for a few minutes, I was able to distill the essence of the idiom(???) used in the code as:

I can’t figure out why B was designed as a class template and not just a plain ole’ class that simply inherits from A (see below). It reminds me a little of the CRTP idiom but it’s not the same. Would you happen to know what problems it is intended to solve? Is it some template meta-programming technique?

  1. January 16, 2011 at 5:48 am

    In and of itself it is not a metaprogramming technique. It can, however, form the basis for one (see mpl::inherit_linearly).

    There’s actually a lot of uses for this kind of thing though admittedly they’re all rather rare to run into. Here’s one example where I apply it: http://stackoverflow.com/questions/4688138/initializing-the-base-class-to-a-derived-base-class/4688402#4688402

    No idea if they used my first or second idea but there you go…an application of the technique.

    • January 16, 2011 at 6:44 am

      Thanks for the valiant try CE, but I’m still confused 🙂

      Thinking out loud: B “is an” A, and B also uses another A for some purpose.

      • January 16, 2011 at 5:29 pm

        Well, perhaps you’d understand better if you grabbed yourself a copy of “Modern C++ Design” by Alexandrescu. He describes “Policy Based Design” and the implementations look very much like the code that’s confusing you. I thought there was an excerpt of that part online but I couldn’t find it anywhere now. The book should be on any C++ developer’s shelf anyway.

  2. January 17, 2011 at 4:26 am

    Ah yes, Mr. “Loki”. I do have his book on my list and am planning to read it after I finish “C++ Templates: The Complete Guide”. Thanks for your help.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: