He's not dead, he's resting

C++ Explicit Template Instantiation Hate

In part two of a never ending series of why I hate C++ but have to use it anyway because there’s nothing else, we come to explicit template instantiation.

Explicit template instantiation is a nuisance that only exists because all build systems suck. Unfortunately, doing everything implicitly makes compiles take way too long, so explicit instantiation is a pragmatic nuisance. So let’s have a header that doesn’t slow down the compiler too much:

#include <string>

template <typename Item_>
struct ItemMaker
    Item_ make_me_an_item() const;

typedef ItemMaker<int> IntMaker;
typedef ItemMaker<std::string> StringMaker;

And then an implementation:

template <typename Item_>
ItemMaker<Item_>::make_me_an_item() const
    return Item_();

But then we need explicit instantiation. Should be no problem, right?

template class IntMaker;
template class StringMaker;

Wrong! Explicit instantiation only works for declarations, not typedefs, so we have to copy things out all over again:

template class ItemMaker<int>;
template class ItemMaker<std::string>;

Yes, thanks for that, Standard guys.


7 responses to “C++ Explicit Template Instantiation Hate

  1. Jason December 27, 2008 at 7:33 am

    Will things become sunnier between you and C++ with C++0x?

    • Ciaran McCreesh December 27, 2008 at 12:28 pm

      C++0x fixes about half the things in C++ that really piss me off, and adds a whole lot more things that are extremely convenient. It’s a major step forward, but it doesn’t fix everything (nor, realistically, would I expect it to).

  2. zs January 13, 2009 at 6:56 am

    but have to use it anyway because there’s nothing else
    oh, realy?
    how about ocaml, or even haskell?

  3. zs January 14, 2009 at 6:44 pm

    does darcs, or xmonad not “real code that’s used by real people”?

  4. Ciaran McCreesh January 14, 2009 at 6:50 pm

    Any programming language where you have to justify it being usable in the real world by providing a short list of examples is unsuitable for use in the real world. If you ask someone for a list of real world programs written in C, C++ or even Java they’ll just laugh at you, not rush out to try their hardest to find an answer.

  5. Pingback: C++ Explicit Template Instantiation Hate Redux « Ciaran McCreesh’s Blag

Leave a Reply

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

You are commenting using your 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 )

Google+ photo

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

Connecting to %s