How do I make this explicit specialization?

Is the following construction possible ?:

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

Now, if that were possible, I would do some explicit specializations for doSomething so that in the end I have some versions, as shown below:

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

which seems impossible I can’t find any syntax for the job, then I thought that the design should be as it should, so that all the template arguments are passed to the template class itself:

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

Then I tried a partial specialization that 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 get the following errors for explicit specialization:
error # 1: the list of template arguments following the class template name should contain a list of parameters that are used in the template parameter list. error # 2: "Container1": too few template arguments.

+6
c ++ templates
source share
6 answers

To explicitly specialize doSomething , you also need to explicitly specialize Test .

From 14.7.3 / 18:

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

+6
source share

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

Only something like this will work:

 template<> template<> void Test<int>::doSomething<int>() { } 
+2
source share

you can always do inline function

 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; }; 
+1
source share

I think this is picky. I suppose you cannot do this, read this .

0
source share

Not sure if this is a bug in g ++, but it compiles and produces what I expect.

 #include<typeinfo> #include<iostream> template<typename T> class Test { public: template<typename Z> void doSomething(); private: T obj; }; template<typename T> template<typename Z> void Test<T>::doSomething() { Z val; std::cout << __func__ << ": type " << typeid(val).name() << std::endl; } int main(int argc, char *argv[]) { Test<double> a; a.doSomething<int>(); a.doSomething<double>(); } 
0
source share

icecrime sent a temporary response, and it compiled due to some error, probably in Visual C ++ 2008:

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

Check out his current answer. The funny thing is that, at least since VC ++ 2008, there is no compilation problem when specializing with inline definitions, but for specializations with non-linear definitions, when there is more than one version, it will not compile successfully.

-2
source share

All Articles