【问题标题】:C++ - how to implement templated member function outside a templated classC++ - 如何在模板化类之外实现模板化成员函数
【发布时间】:2011-01-28 21:46:44
【问题描述】:
template<int N>
class myClass
{
    template<typename T>
    void myFunction();
};

template<typename T>
void myClass<int N>::myFunction() {} // doesn't work, nor do many other combinations!

嗨,

是否有可能实现上述目标?我可以在类定义中实现 myFunction 没问题。如果是这样,语法是什么? GCC 4.2 告诉我:

缺少'>'来终止模板参数列表

感谢您的帮助

【问题讨论】:

  • 您的问题可能是模板 吗?它应该是 template no?
  • @Colin:它是一个非类型模板参数。

标签: c++ templates


【解决方案1】:

您正在寻找:

template <int N>
template <typename T> 
void myClass<N>::myFunction() {} 

类模板需要一个template,成员函数模板需要一个。

【讨论】:

    【解决方案2】:
    template<int N> template<typename T>
    void myClass<N>::myFunction() {}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多