Jonathan Boccara's blog

Template Partial Specialization In C++

Published August 11, 2017 - 4 Comments

Daily C++

Today I want to share something about the right way to emulate partial function template specialisation in C++. I learnt it by watching Arthur O’Dwyer’s CppCon talk Template Normal Programming.

Actually, the technique for emulating function template partial specialization through class template specialization is well known, but the naming convention used by Arthur is the clearest I’ve seen. And he has kindly accepted that I share it with you on Fluent C++. Jump to the bottom of the post to get to it directly.

All this is a great opportunity for a general review of template partial specialization in C++.

Partial Template Specialization

C++ allows to partially specialize class templates:

In the above code, is_pointer has a primary template (the first struct) and a specialization (the second one). And since the specialization still has a template parameter, it is called a partial specialization.

Partial specialization also works for variable templates:

But C++ forbids partial specialization on anything else than classes (or structs) and variables.

That means that alias template partial specialization is forbidden. So the following code is also invalid:

In fact, even total specialization of alias templates is forbidden.

And while function templates can be totally specialized, their partial specialization is illegal. So the following code:

leads to a compilation error.

Why can’t we partially specialize everything?

To be honest, I don’t know. But I wonder.

Herb Sutter touches upon the subject in More Exceptional C++ Item 10 and in Exceptional C++ Style Item 7, but it’s more about total specialization than partial. I suspect that the rationale for functions partial specialization is that it would allow mixing partial specializations with overloading, which would become too confusing.

Does anyone know the reason for restricting on function template specialization?

For aliases this answer on Stack Overflow gives some elements of information. In short, the using declaration is no more than an alias, that shouldn’t fetaure more logic. This explains why even total specialization are not allowed for aliases.

Emulating Partial Template Specialization

To emulate partial specialization on aliases and on functions, the technique is to fall back on the specialization that works on structs.

Here is how to go about it for alias templates:

As for functions templates, let me share the implementation that Arthur O’Dwyer’s CppCon demonstrates in his talk. He also uses a fall back on structs, but his naming conventions is the clearest that I’ve seen:

Did you notice the name of the static function inside the structs? It’s just an underscore, which is a legal name for a function in C++. Since these functions are just a technical artifact, I think it’s good to show that they carry no meaning by giving them (almost) no name.

With this you can emulate any missing template partial (or even total for aliases) specialization in C++.

Regardless of this, Template Normal Programming is a great talk, that shows you everything there is to know about templates, except in metaprogramming. It’s definitely worth watching.

 

Share this post! Facebooktwittergoogle_pluslinkedin    Don't want to miss out ? Follow:   twitterlinkedinrss

Receive regular updates to make your code more expressive.