c++ - 更大类的单个方法的部分模板特化

标签 c++ templates

我有以下类(class);

template<int N, int M, int K>
class BaumWelch
{
  //lots of stuff
  const TransitionMatrixTemplate<N, M> randomA()
  { //.... }
}

现在我想专门针对 N=1 的方法 randomA。我该怎么做?

我试着回答这个问题:Template specialization of a single method from a templated class ,但它似乎不适用于部分特化。本题:C++ partial method specialization似乎更相关,但它建议对整个类(class)进行特化(对我来说这相当大)。是否可以特化整个类,但实际上只特化这个方法?

最佳答案

I would like to specialize the method randomA for N=1. How can I do it?

您发现函数的偏特化是不允许的。

但是,您可以完全专门化代码的“详细”实现。

  template<int TheN>
  detail_randomA();

  const TransitionMatrixTemplate<N, M> randomA()
  {
      return detail_randomA<N>();
  }

在类声明之外:

  template<int N, int M, int K>
  template<int TheN>
  BaumWelch<N,M,K>::detail_randomA()
  {
      //lots of stuff when N != 1
  }

  template<int N, int M, int K>
  template<>
  BaumWelch<N,M,K>::detail_randomA<1>()
  {
      //lots of stuff when N == 1
  }

关于c++ - 更大类的单个方法的部分模板特化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16174036/

相关文章:

c++ - Axis/C MIME/DIME 和 MTOM 入门

c++ - 如何使用较旧的 C++ 标准编译 Boost? (特别是 C++03)

c++ - 在 C++ 中将函数模板作为参数传递

c++ - 在模板类中使用模板函数

c++ - 使用模板函数继承类

c++ - 在派生类上应用父类(super class)测试

c++ - C++11 中 map 标准的 at() const 访问器是什么?

c++ - 在 C++ 库中有错误代码选项以提高性能

c++ (g++-4.x) 模板问题

templates - 创建的 PDF 不反射(reflect)对原始文档所做的更改