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"); } };
vladr
source share