完全特化 class 模板的函数模板的显式特化

Explicit specialization of a function template for a fully specialized class template

我正在尝试在 class 模板的特化中特化一个函数,但无法找到正确的语法:

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

这里我尝试将 fn 专门化为 char,它位于 Foo 专门化为 int 的内部。但是编译器不喜欢我写的东西。那么正确的语法应该是什么?

你不必说你在专精两次。

您在这里只专精一个函数模板

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>();
}