Explicit function template specialization for a fully specialized class template

I am trying to specialize a function as part of a class template specialization, but I cannot determine the correct syntax:

template< typename T > struct Foo {}; template<> struct Foo< int > { template< typename T > void fn(); }; template<> template<> void Foo< int >::fn< char >() {} // error: too many template-parameter-lists 

Here I am trying to specialize fn for char , which is inside Foo specialized for int . But the compiler does not like what I am writing. What should be the correct syntax?

+5
source share
1 answer

You do not need to say that you specialize twice.

Here you specialize in only one function template.

 template<> void Foo<int>::fn<char>() {} 

Live on coliru

 template< typename T > struct Foo {}; template<> struct Foo< int > { template< typename T > void fn(); }; template<> void Foo<int>::fn<char>() {} int main() { Foo<int> f; f.fn<char>(); } 
+6
source

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


All Articles