Default template argument and partial specialization

The default argument applies to the specialization — and, in fact, a specialization must accept (so to speak) the base template’s default argument(s). Attempting to specify a default in the specialization: template<class A = int, class B=double> class Base {}; template<class B=char> // … …is an error. Likewise, if we change the specialization so that … Read more

Wrong specialized generic function gets called in Swift 3 from an indirect call

This is indeed correct behaviour as overload resolution takes place at compile time (it would be a pretty expensive operation to take place at runtime). Therefore from within test(value:), the only thing the compiler knows about value is that it’s of some type that conforms to DispatchType – thus the only overload it can dispatch … Read more

static member initialization for specialized template class

For static member specializations, if you don’t initialize the member, it is taken as a specialization declaration, that just says “Oh, don’t instantiate the member from the primary template, because there is a specialized definition somewhere else”. It should be mentioned that the definition should appear in a .cpp file (otherwise, you will earn the … Read more

Template specialization of a single method from a templated class

As with simple functions you can use declaration and implementation. Put in your header declaration: template <> void TClass<int>::doSomething(std::vector<int> * v); and put implementation into one of your cpp-files: template <> void TClass<int>::doSomething(std::vector<int> * v) { // Do somtehing with a vector of int’s } Don’t forget to remove inline (I forgot and thought this … Read more

Template specialization of particular members?

You can only specialize it explicitly by providing all template arguments. No partial specialization for member functions of class templates is allowed. template <typename T,bool B> struct X { void Specialized(); }; // works template <> void X<int,true>::Specialized() { … } A work around is to introduce overloaded functions, which have the benefit of still … Read more

Selecting a member function using different enable_if conditions

enable_if works because the substitution of a template argument resulted in an error, and so that substitution is dropped from the overload resolution set and only other viable overloads are considered by the compiler. In your example, there is no substitution occurring when instantiating the member functions because the template argument T is already known … Read more

c++ template partial specialization member function [duplicate]

You cannot partially specialize only a single member function, you must partially specialize the whole class. Hence you’ll need something like: template <typename T> class Object<T, 0> { private: T m_t; Object(); public: Object(T t): m_t(t) {} T Get() { return m_t; } Object& Deform() { std::cout << “Spec\n”; m_t = -1; return *this; } … Read more

explicit specialization of template class member function

It doesn’t work that way. You would need to say the following, but it is not correct template <class C> template<> void X<C>::get_as<double>() { } Explicitly specialized members need their surrounding class templates to be explicitly specialized as well. So you need to say the following, which would only specialize the member for X<int>. template … Read more