Template Specialization for a function without Parameters

22,491

Solution 1

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>() { }

Solution 2

The correct syntax for the primary template is:

template <typename T>
void doStuff() {}

To define a specialisation, do this:

template <>
void doStuff<DefinedClass>() { /* function body here */ }

Solution 3

I guess that is not the correct syntax (since it is not compiling). How should I do it? doStuff will use T wihtin its body to declare a variable.

template<typename T>  
void doStuff() 
{
  T t = T();   // declare a T type variable

}

would it be possible to declare the body in a .cpp?

C++ only supports inclusive mode only, you can't compile separately then link later.

From comment, if you want to specialize for int type:

template<>
void doStuff<int>()
{
}
Share:
22,491

Related videos on Youtube

Mario Corchero
Author by

Mario Corchero

C++ Software Engineer at Bloomberg LP. My wider grasp resides in C++, OO PL and Databases. Although I am still truly newbie! As a hobby I have developed some Web using mainly HTML5 and JQuery with some PHP, .Net or Django on server side. I really love software design, architecture and C++ crazy caveats!

Updated on July 09, 2022

Comments

  • Mario Corchero
    Mario Corchero almost 2 years

    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.

    • Kerrek SB
      Kerrek SB over 11 years
      When you say "it is not compiling", you should include the relevant error message.
    • Christian Rau
      Christian Rau over 11 years
      Does template<typename T> void doStuff<T>() {} even compile in the first place (I think this <T> is invalid).
  • Mario Corchero
    Mario Corchero over 11 years
    Thanks, but still not working. If I write the specialization like: "template<> void <int>doStuff();" I got: "invalid explicit specialization before '>' token"
  • Mario Corchero
    Mario Corchero over 11 years
    If I put" template<typedef T>" I get "template-id `buildBom<int>' in declaration of primary template"
  • Synxis
    Synxis over 11 years
    No, it should be template<> void doStuff<int>(); for specialization and template<typename T> void doStuff(); for general case...
  • Kerrek SB
    Kerrek SB over 11 years
    @Synxis: Thanks! I added that example, and I removed the ... from the first set, because you can't partially specialize functions anyway. Sometimes a less general answer is more helpful :-)