Home > C++14 > Stopping The Spew!

Stopping The Spew!

Every C++ programmer has experienced at least one, and most probably many, “Template Spew” (TS) moments. You know you’ve triggered a TS moment when, just after hitting the compile button on a program the compiler deems TS-worthy, you helplessly watch an undecipherable avalanche of error messages zoom down your screen at the speed of light. It is rumored that some novices who’ve experienced TS for the very first time have instantaneously entered a permanent catatonic state of unresponsiveness. It’s even said that some poor souls have swan-dived off of bridges to untimely deaths after having seen such carnage.

Note: The graphic image that follows may be highly disturbing. You may want to stop reading this post at this point and continue to waste company time by surfing over to facebook, reddit, etc.

TS occurs when one tries to use a templated class object or function template with a template parameter type that doesn’t provide the behavior “assumed” by the class or function. TS is such a scourge in the C++ world that guru Scott Meyers dedicates a whole item in Effective STL“, number 49, to handling the trauma associated with deciphering TS gobbledygook.

For those who’ve never seen TS output, here is a woefully contrived example:


The above example doesn’t do justice to the havoc the mighty TS dragon can wreak on the mind because the problem (std::vector<T> requires its template arg to provide a copy assignment function definition) can actually be inferred from a couple of key lines in the sea of TS text.

Ok, enough doom and gloom. Fear not, because help is on the way via C++14 in the form of “concepts lite“. A “concept” is simply a predicate evaluated on a template argument at compile time. If you employ them while writing a template, you inform the compiler of what kind(s) of behavior your template requires from its argument(s). As a use case illustration, behold this slide from Bjarne Stroustrup:


The “concept” being highlighted in this example is “Sortable“. Once the compiler knows that the sort<T> function requires its template argument to be “Sortable“,  it checks that the argument type is indeed sortable. If not, the error message it emits will be something short, sweet, and to the point.

The concept of  “concepts” has a long and sordid history. A lot of work was performed on the feature during the development of the C++11 specification. However, according to Mr. Stroustrup, the result was overly complicated (concept maps, new syntax, scope & lookup issues). Thus, the C++ standards committee decided to controversially sh*tcan the work:

C++11 attempt at concepts: 70 pages of description, 130 concepts – we blew it!

After regrouping and getting their act together, the committee whittled down the number of pages and concepts to something manageable enough (approximately 7 pages and 13 concepts) to introduce into C++14. Hence, the “concepts lite” label. Hopefully, it won’t be long before the TS dragon is relegated back to the dungeon from whence it came.

  1. George
    May 15, 2014 at 7:40 am

    Clang isn’t so bad. There’s some spew, but the last two errors make it clear what’s happening:

    ts.cpp:11:11: note: in instantiation of member function
    ‘std::__1::vector<MyType, std::__1::allocator >::operator=’
    requested here
    myStuff2 = myStuff1;
    ts.cpp:4:10: note: candidate function has been explicitly deleted
    MyType& operator=(const MyType& mt) = delete;

    • May 15, 2014 at 7:45 am

      Blushingly, I haven’t used Clang, but I’ve heard the same thing.

  2. May 16, 2014 at 3:06 am

    Concepts Light will not be a part of C++14. It is developed in a TS (Technical Specification, think of it as a feature branch, http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3929.pdf), which can be incorporated into C++17.
    We hope to see implementations across major compilers earlier than that though.

    • May 16, 2014 at 5:54 am

      Thanks for setting me straight Fabio. I heard Bjarne say they’re currently implemented in an experimental branch of GCC.

      • fabio
        May 16, 2014 at 6:30 am

        Yes, they are. Many features are prototyped before they are standardized (the committee looks out for implementation experience before standardizing something, especially such complex features as Concepts). The old C++0x Concepts also had such a prototype.
        My impression is that the Concepts Light branch is much closer to production ready than the C++0x one ever was, though.

        Anyway the idea of the TS’s is that multiple compiler Vendors have a common document to implement and experiment with not-quite-standard features, and make sure that they are experimented with and used by a wider audience, before they get standardized in their final form.
        So we hope to see Concepts Light form other vendors like clang and vc soon, too.

  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 )

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: