Jonathan Boccara's blog

Function Templates Partial Specialization in C++

Published August 15, 2017 - 8 Comments

Daily C++

Why doesn’t C++ allow partial specialization on function templates? Such was the question I asked to you, readers of Fluent C++, in the post covering Template Partial Specialization. Not because I wanted to test you, but simply because I couldn’t find the answer.

And oh boy did I get an answer.

The post received comments, questions, suggestions and discussions, and even if the article covered template partial specialization in general, most of the reactions revolved around the subject of function template partial specialization. And I want to thank /u/sphere991/u/quicknir, rr0ki and Simon Brand in particular for their contributions.

Each comment pointed to a particular facet of the subject. Now what I want to share with you is how, put together, they allowed me to take a step back and have a wider vision on function template partial specialization. I’m excited to share this big picture with you because it’s exactly what I wished I could read when I was searching for more info on this, and that I couldn’t find anywhere.

template function partial specialization

Function specialization? Just overload!

When you think about it, template specialization comes down to picking the right implementation for a given type. And the resolution occurs at compile time.

Now compare this with function overloading: it consists in picking the right function for a given type of argument. And the resolution also occurs at compile time.

In this perspective, these two features look very similar. Therefore, it is only normal that you can achieve something equivalent to function template (partial or total) specialization with function overloading. Let’s illustrate with an example.

Consider the following template function f:

Say that we want a specific implementation when T is std::string.

We could either specialize f:

or we could simply overload:

Either way, the execution will go through the specific implementation when you pass f a string.

The same goes for partial specialization. Say we want a specific implementation of f for vectors. We can’t write it with partial specialization since the following would be illegal:

But we can write it with overloading instead:

and we get the desired effect just as well.

What if you can’t overload

Is this to say that there is no case for partial specialization on template functions? No! There are cases where overloading won’t do.

Overloading works for template arguments, which arguably represent a fair share of use cases for template functions. But what if the template is not in the argument? For instance, it could be in the return type of the function:

Or it could even be nowhere in the function prototype:

The common point between those cases is that you have to specify the template type explicitly at call site:

In such cases, overloading can’t do anything for us, so for those cases I believe there is a case for partial specialization on function templates. Let’s review our options for working around the fact that C++ does not support it natively.

Fall back on overloading

This is the technique that Simon Brand proposed. It consists in adding a parameter that carries the information about what type T is. This parameter, type, is something that just carries another type T:

(we could also omit the name T here since it isn’t used the body of the template.)

This allows to return to the case where we can use overloading instead of specialization.

Consider the following example to illustrate. We want to design a template function create that returns an object of type T initialized by default:

except when the type to be returned is a vector, in which case we want to allocate a capacity of 1000 to anticipate for repeated insertions:

So we want a default implementation, and one for all vector<T> for all T. In other terms, we need to partially specialize f with vector<T>.

Here is how to achieve this with this technique:

Fall back on class template partial specialization

Even if we can’t do partial specialization on function template, we can do it for class templates. And there is a way to achieve the former by reusing the latter. To see the cleanest way to do this, you can refer to the post Template Partial Specialization where I get into this (and more) in details.

Whatever you do, don’t mix specialization and overloading

When you use a set of overloads for several implementations of a function, you should be able to understand what’s going on.

When you use a set of (total) specializations for several implementations of a function, you still should be able to understand what’s going on.

But when you mix both overloading and specializations for the same function, you’re entering the realm where the magic, the voodoo and the Bermuda Triangle live, a world where things behave in an inexplicable way, a world where you’re better off not knowing too many explanations about because they could suck you in it and your mind will end up in a plane cemetery and pierced with spikes carrying dark magic spells.

To illustrate, consider this insightful example given by /u/sphere991 that says it all:


The mere order of declarations of the overloads and the specializations determines the behaviour of the call site. Reading this piece of code sends a chill down my spine. Brrrr.

Whichever technique you choose, don’t mix function overloading and function specialization for the same function name.

Will C++ support function template partial specialization?

We’ve seen when we need partial specialization for function templates and how to emulate it. But we haven’t answered our original question: Why doesn’t C++ allow partial specialization on function templates?

The only element of answer that I got was provided to me by rr0ki in this comment, referring to an old document written by Alistair Meredith. If I summarize what this document says, the feature has been considered a while ago, and left out because the concept_maps could do the job instead. But concept_maps are part of the version of concepts that was abandoned since then!

The way I interpret this is that there is nothing wrong in allowing function template partial specialization in C++, but we don’t know if it will be in the language one day.

Related articles:

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

Get a free ebook on C++ smart pointers

Get a free ebook of more than 50 pages that will teach you the basic, medium and advanced aspects of C++ smart pointers, by subscribing to our mailing list! On top of that, you will also receive regular updates to make your code more expressive.

No spam. You can unsubscribe at any time.