How to specialize a template class member with a template template parameter

I have a template template with an int template and template parameter. Now I want to specialize a member function:

template <int I> class Default{}; template <int N = 0, template<int> class T = Default> struct Class { void member(); }; // member definition template <int N, template<int> class T> inline void Class<N, T>::member() {} // partial specialisation, yields compiler error template <template<int> class T> inline void Class<1, T>::member() {} 

Can someone tell me if this is possible and what am I doing wrong on the last line?

EDIT: I want to thank everyone for their contribution. Since I also need specialization for some T, I decided to abandon the workaround proposed by Nawaz, and specialized the whole class, because in any case it had only one member function and one data element.

+6
source share
4 answers

You cannot partially specialize one member function, you have to do this for the whole class.

 template <int I> class Default{}; template <int N = 0, template<int> class T = Default> struct Class { void member(); }; // member definition template <int N, template<int> class T> inline void Class<N, T>::member() {} // partial specialization template <template<int> class T> struct Class<1, T> { void member() {} }; 
+6
source

Since this is unacceptable, here is one way:

 template <int I> class Default{}; template <int N = 0, template<int> class T = Default> struct Class { void member() { worker(int2type<N>()); //forward the call } private: template<int N> struct int2type {}; template<int M> void worker(const int2type<M>&) //function template { //general for all N, where N != 1 } void worker(const int2type<1>&) //overload { //specialization for N == 1 } }; 

The idea is that when N = 1, the call to worker(int2type<N>()) will be resolved to the second function (specialization), because we pass an instance of type int2type<1> . Otherwise, the first, general, function will be solved.

+3
source

In C ++, you are not allowed to partially specialize a function; you can only partially specialize classes and structures. I believe this applies to member functions as well.

+2
source

Check this article: http://www.gotw.ca/publications/mill17.htm

It is quite small and has good code examples. This will explain the problem by specializing the patent template template and show other ways to use it.

+2
source

Source: https://habr.com/ru/post/898093/


All Articles