Question

template <typename T>
void foo(T t)
{
   ... // do stuff with type T
}

template <typename T>
class class_template
{
    // class body
};

template<>                       // failed attempt at full specialization
void foo(class_template<T> t)    // which doesn't work of course
{
    //full specialization for all classes of class_template
} 

In the above code how do I explicitly specialize function foo with a class template?

Was it helpful?

Solution

In the above code how do I explicitly specialize function foo with a class template?

You cannot. This is the whole point of partial specialisations. But they don’t work for functions.

You have two solutions:

  • Overload the function. This usually works.
  • Refer the work to a class template, which can be partially specialised. That is, inside your function, call a (static) function in a class template, and specialise that.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top