Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I do this explicit specialization?

Is following design possible?:

template <typename T>
class Test{
 public:
  template <typename Z>
  void doSomething();
  //rest of things
 private:
  T obj;
  //some things
};

Now if it was possible I'd do some explicit specializations for doSomething so that at the end I'd have some versions like below:

void doSomething<int>(){
 //do something
}
void doSomething<double>(){
 //do something
}
...etc

which seems impossible I can't find any syntax to do the job then I thought maybe the design should be as it follows so that all template arguments should be passed to template class itself:

template <typename T,typename Z>
class Test{
 public:
  void doSomething();
  //rest of things
 private:
  T obj;
  //some things
};

Then I tried partial specialization which didn't even compile:

template <typename T>
void Test<T,int>::doSomething(){
 //do something
}
template <typename T>
void Test<T,double>::doSomething(){
 //do something
}
...etc

I got the following errors for explicit specialization:
error#1:template argument list following class template name must list parameters in the order used in template parameter list.
error#2:'Container1' :too few template arguments.

like image 729
Pooria Avatar asked Nov 25 '10 08:11

Pooria


People also ask

What is the syntax for explicit class specialization?

What is the syntax to use explicit class specialization? Explanation: The class specialization is creation of explicit specialization of a generic class. We have to use template<> constructor for this to work. It works in the same way as with explicit function specialization.

What is template specialization used for?

It is possible in C++ to get a special behavior for a particular data type. This is called template specialization. Template allows us to define generic classes and generic functions and thus provide support for generic programming.

What is the other name of full specialization?

Explanation: explicit specialization is another name of full specialization.

What is a specialization C++?

The act of creating a new definition of a function, class, or member of a class from a template declaration and one or more template arguments is called template instantiation. The definition created from a template instantiation is called a specialization.


3 Answers

In order to explicitly specialize doSomething you have to also explicitly specialize Test.

From 14.7.3/18 :

In an explicit specialization declaration for a member of a class template or a member template that appears in namespace scope, the member template and some of its enclosing class templates may remain unspecialized, except that the declaration shall not explicitly specialize a class member template if its enclosing class templates are not explicitly specialized as well.

like image 115
icecrime Avatar answered Sep 22 '22 21:09

icecrime


You cannot explicitly specialize a member template unless its enclosing class templates are also explicitly specialized.

So only something like this will work:

template<> template<>
void Test<int>::doSomething<int>()
{
}
like image 39
Nikola Smiljanić Avatar answered Sep 24 '22 21:09

Nikola Smiljanić


you can always make the function inline

template <class T>
class Test
{
public:
 template <class Z>
 void doSomething() { cout << "default" << endl; }

 template<>
 void doSomething<int>() { cout << "int" << endl;}
 template<>
 void doSomething<double>() { cout << "double" << endl; }
private:
 T obj;
};
like image 21
hidayat Avatar answered Sep 23 '22 21:09

hidayat