Template Specialization for a function without Parameters

Mario Corchero picture Mario Corchero · Feb 8, 2013 · Viewed 19.9k times · Source

I need to specialize a function template in c++.

template<typename T>  
void doStuff<T>() {}

To

template<>
void doStuff<DefinedClass>();

and

template<>
void doStuff<DefinedClass2>();

I guess that is not the correct syntax (since it is not compiling). How should I do it?
Also, Since I will have not undefined template parameters in doStuff<DefinedClass>, would it be possible to declare the body in a .cpp?

Note: doStuff will use T wihtin its body to declare a variable.

Answer

Kerrek SB picture Kerrek SB · Feb 8, 2013

The primary template doesn't get a second pair of template arguments. Just this:

template <typename T> void doStuff() {}
//                        ^^^^^^^^^

Only the specializations have both a template <> at the front and a <...> after the name, e.g.:

template <> void doStuff<int>() { }