【问题标题】:Explicit specialization of a function template for a fully specialized class template完全专业化的类模板的函数模板的显式专业化
【发布时间】:2015-11-27 00:41:43
【问题描述】:

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

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。但是编译器不喜欢我写的东西。那么正确的语法应该是什么?

【问题讨论】:

    标签: c++ templates template-specialization


    【解决方案1】:

    您不必说自己专精两次。

    你只是在这里专门一个函数模板

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

    【讨论】:

    • 令人惊讶的是,编译器的错误消息是正确的。
    猜你喜欢
    • 1970-01-01
    • 2011-01-18
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 2018-09-28
    • 1970-01-01
    相关资源
    最近更新 更多