Multiple SFINAE class template specialisations using void_t

There is a rule that partial specializations have to be more specialized than the primary template – both of your specializations follow that rule. But there isn’t a rule that states that partial specializations can never be ambiguous. It’s more that – if instantiation leads to ambiguous specialization, the program is ill-formed. But that ambiguous … Read more

Why is it not possible to overload class templates?

Section 12.5 from Templates the Complete Guide (Amazon) contains this quote: You may legitimately wonder why only class templates can be partially specialized. The reasons are mostly historical. It is probably possible to define the same mechanism for function templates (see Chapter 13). In some ways the effect of overloading function templates is similar, but … Read more

C++ templates specialization syntax

Here are comments with each syntax: void foo(int param); //not a specialization, it is an overload void foo<int>(int param); //ill-formed //this form always works template <> void foo<int>(int param); //explicit specialization //same as above, but works only if template argument deduction is possible! template <> void foo(int param); //explicit specialization //same as above, but works … Read more

Specialization of templated member function in templated class

Yes, this is the problem: error: enclosing class templates are not explicitly specialized You cannot specialize a member without also specializing the class. What you can do is put the code from function in a separate class and specialize that, much like basic_string depends on a separate char_traits class. Then then non-specialized function can call … Read more

Will specialization of function templates in std for program-defined types no longer be allowed in C++20?

As it stands now it definitly looks that way. Previously [namespace.std] contained A program may add a template specialization for any standard library template to namespace std only if the declaration depends on a user-defined type and the specialization meets the standard library requirements for the original template and is not explicitly prohibited. While the … Read more

Why function template cannot be partially specialized?

AFAIK that’s changed in C++0x. I guess it was just an oversight (considering that you can always get the partial specialization effect with more verbose code, by placing the function as a static member of a class). You might look up the relevant DR (Defect Report), if there is one. EDIT: checking this, I find … Read more

C++ function template partial specialization?

Function partial specialization is not yet allowed as per the standard. In the example, you are actually overloading & not specializing the max<T1,T2> function. Its syntax should have looked somewhat like below, had it been allowed: // Partial specialization is not allowed by the spec, though! template <typename T> inline T const& max<T,T> (T const& … Read more

Template Specialization VS Function Overloading

Short story: overload when you can, specialise when you need to. Long story: C++ treats specialisation and overloads very differently. This is best explained with an example. template <typename T> void foo(T); template <typename T> void foo(T*); // overload of foo(T) template <> void foo<int>(int*); // specialisation of foo(T*) foo(new int); // calls foo<int>(int*); Now … Read more