Template class, function specialization

I want to have a template class that looks something like the one below. Then I want it to have a function using a specialized template depending on the CLASS template parameter. How to do it? I understand that the code I presented is erroneous at many levels, but this is just to illustrate the concept.

template <typename _T, size_t num> class Foo { // If num == 1, I want to call this function... void Func<_T, 1>() { printf("Hi!"); } // Otherwise, I want to call this version. void Func<_T, num>() { printf("Hello world!"); } }; 
+6
c ++ templates
source share
3 answers
 struct Otherwise { }; template<size_t> struct C : Otherwise { }; // don't use _Uppercase - those names are reserved for the implementation // (i removed the '_' char) template <typename T, size_t num> class Foo { public: void Func() { Func(C<num>()); } private: // If num == 1, I want to call this function... void Func(C<1>) { printf("Hi 1!"); } // If num == 2, I want to call this function... void Func(C<2>) { printf("Hi 2!"); } // Otherwise, I want to call this version. void Func(Otherwise) { printf("Hello world!"); } //// alternative otherwise solution: // template<size_t I> // void Func(C<I>) { .. } }; 
+9
source share

There are no partial specializations of function templates and partially specializing the member that you need to partially specialize the class template.

 template< typename _T, size_t num > struct Foo { void Func() { printf("Hello world!"); } }; template< typename _T > struct Foo< _T, 1 > { void Func() { printf("Hi!"); } }; 

Now, if Foo also contains methods other than Func , the implementation of which does not depend on the value for num , and you do not want to duplicate their implementation according to the Foo specialization, you can apply the following template:

 template< typename _T, size_t num > struct FooFuncBase { void Func() { printf("Hello world!"); } }; template< typename _T > struct FooFuncBase< _T, 1 > { void Func() { printf("Hi!"); } }; template< typename _T, size_t num > struct Foo : public FooFuncBase< _T, num > { void OtherFuncWhoseImplementationDoesNotDependOnNum() { ... } }; 

Or using CRTP :

 template< typename _Derived, typename _T, size_t num > struct FooFuncBase { void Func() { static_cast< _Derived* >(this)->OtherFuncWhoseImplementationDoesNotDependOnNum(); printf("Hello world!"); } }; template< typename _Derived, typename _T > struct FooFuncBase< _Derived, _T, 1 > { void Func() { static_cast< _Derived* >(this)->OtherFuncWhoseImplementationDoesNotDependOnNum(); printf("Hi!"); } }; template< typename _T, size_t num > struct Foo : public FooFuncBase< Foo< _T, num >, _T, num > { void OtherFuncWhoseImplementationDoesNotDependOnNum() { printf("Other"); } }; 
+2
source share

This is called partial pattern specialization. It looks like this:

 template<typename _T, size_t num> class FooBase { }; template <typename _T, size_t num> class Foo : public FooBase<_T,num> { void Func() { printf("Hello world!"); } }; template <typename _T> class Foo<_T,1> : public FooBase<_T,num> { void Func() { printf("Hi!"); } } 
+1
source share

All Articles