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 disallowed for partial specialization in a non-type argument to use nested template parameters

I think a lot of it is historical. Non-type template parameters weren’t originally allowed at all. When they were added, there were lots of limitations. As people tried different possibilities, and confirmed that they didn’t cause problems, some of the limitations were removed. Some of those original limitations remain for no particular reason beyond the … Read more

(Partially) specializing a non-type template parameter of dependent type

See paragraph [temp.class.spec] 14.5.5/8 of the standard: The type of a template parameter corresponding to a specialized non-type argument shall not be dependent on a parameter of the specialization. [ Example: template <class T, T t> struct C {}; template <class T> struct C<T, 1>; // error template< int X, int (*array_ptr)[X] > class A … Read more

How to do template specialization in C#

In C#, the closest to specialization is to use a more-specific overload; however, this is brittle, and doesn’t cover every possible usage. For example: void Foo<T>(T value) {Console.WriteLine(“General method”);} void Foo(Bar value) {Console.WriteLine(“Specialized method”);} Here, if the compiler knows the types at compile, it will pick the most specific: Bar bar = new Bar(); Foo(bar); … 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

“invalid use of incomplete type” error with partial template specialization

You can’t partially specialize a function. If you wish to do so on a member function, you must partially specialize the entire template (yes, it’s irritating). On a large templated class, to partially specialize a function, you would need a workaround. Perhaps a templated member struct (e.g. template <typename U = T> struct Nested) would … 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