user1077392
user1077392

Reputation: 21

Why doesn't my program work when I try to partially specialize a function template?

I am an beginner in template metaprogramming trying to implement generation of multiple versions of similar but slightly different code:

#include <iostream>

enum Type
{
   a1,
   a2
};
enum Style
{
   b1,
   b2
 };

 template<Type,Style> 
 void doSomething();
 {
   std::cout<<" some rubbish\n";
 };

Full specialization works well:

template<> 
void doSomething<a1,b2>()
{
   std::cout<<" this is my template parameters one :" <<a1<< " and  two:"<<b2<<std::endl;
 }

int main(int argc, char* argv[])
{
  doSomething<a1,b1>();
  doSomething<a1,b2>();

   return 0;
}

:some rubbish

:this is my template parameters one :0 and two:1

But partial specialization like below fails:

 template<Style Some> 
  void doSomething<a1,Some>()
 {
  // here I want to use sub-template on some: e.g do_other<Some>
 }

with error: error C2768: 'DoSomething' : illegal use of explicit template arguments

(Body of generic template is commented in this case, though it does not make any difference)

Such specialization is in all samples for partial specialization, but does not work for me. This confuses me a lot.

Very appreciative for any suggestions

Upvotes: 2

Views: 203

Answers (2)

nikola-miljkovic
nikola-miljkovic

Reputation: 670

C++ Priorities functions that uses specialization for your types, rest is handled by normal template function.

Upvotes: 0

parapura rajkumar
parapura rajkumar

Reputation: 24403

In C++ you cannot do partial specialization for functions. Consider moving your functions to a class.

Upvotes: 2

Related Questions