完全专用的类模板的函数模板的显式特化

dra*_*oot 5 c++ templates template-specialization

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

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
Run Code Online (Sandbox Code Playgroud)

在这里,我想专门fnchar,这是内部Foo专门为int.但是编译器不喜欢我写的东西.什么应该是正确的语法呢?

seh*_*ehe 6

你不必说你专攻两次.

你只是在这里专门化一个功能模板

template<> void Foo<int>::fn<char>() {}
Run Code Online (Sandbox Code Playgroud)

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>();
}
Run Code Online (Sandbox Code Playgroud)