【问题标题】:Partial template specialization of a single method of a bigger class更大类的单个方法的部分模板特化
【发布时间】:2013-04-16 22:39:51
【问题描述】:

我有以下课程;

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

现在我想将方法​​ randomA 专门用于 N=1。我该怎么做?

我尝试关注这个问题:Template specialization of a single method from a templated class,但它似乎不适用于部分专业化。这个问题:C++ partial method specialization 似乎更相关,但它建议专攻整个班级(这在我的情况下相当大)。是否可以专门化整个类,但实际上只专门化这一种方法?

【问题讨论】:

    标签: c++ templates


    【解决方案1】:

    我想将方法​​ randomA 专门用于 N=1。我该怎么做?

    您发现不允许对函数进行部分特化。

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

      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
      }
    

    【讨论】:

    • 或者,您可以分派到另一个 detail 实现并专门化它。
    • int buffer[N-1] = {0}; 不会在您的解决方案下编译。
    • @Yakk 正确(或者它不应该)。问题中是否为此功能提出了该代码?我现在没看到,但也许有修改?
    • @DrewDormann 不,没有提供实现。但是if 解决方案确实不 意味着您不需要专业化(这是您在回答中声称的)。您可能不需要专业化,或者您可能需要它。
    • 不起作用 :(。在 else 分支中,我除以 (N-1) 并且 gcc 抱怨有除以零。而且我认为它实际上会抱怨(带有高度警告至少级别)if 语句始终为真/假(取决于当前实例化)。
    【解决方案2】:

    您不能部分专门化 function 模板,但您可以将工作交给 class 模板。这是一个完整的示例:

    #include<iostream>
    using namespace std;
    
    // This first template isn't important, it's just the return value from your function
    template <int N, int M>
    struct TransitionMatrixTemplate {
            void print_me() const {
                    cout << N << ',' << M << endl;
            }
    };
    
    // We need to announce the existence of the BaumWelch class template early here,
    // in order that it can appear in the signature of our impl_randomA class.
    template<int N, int M, int K>
    struct BaumWelch;
    
    // Now, the first important bit of code.  The default implementation
    template<int N, int M, int K>
    struct impl_randomA {
            static TransitionMatrixTemplate<N,M> f(BaumWelch<N,M,K> * This) {
                    return TransitionMatrixTemplate<N,M>();
            }
    };
    
    // Next, is the partially specialized version.
    template<int M, int K>
    struct impl_randomA<1,M,K> {
            static TransitionMatrixTemplate<1,M> f(BaumWelch<1,M,K> * This) {
                    cout << "<Special for N=1> ";
                    return TransitionMatrixTemplate<1,M>();
            }
    };
    
    // Finally, The BaumWelch class and its call out to impl_randomA.
    template<int N, int M, int K>
    struct BaumWelch {
            const TransitionMatrixTemplate<N, M> randomA() {
                    return impl_randomA<N,M,K> :: f(this);
            }
    };
    
    int main() {
            BaumWelch<2,3,4>() . randomA() . print_me();
            BaumWelch<1,3,4>() . randomA() . print_me();
    }
    

    【讨论】:

    • 这看起来很有希望。我应该如何在.h 和.cpp 文件之间拆分它?
    • @Grzenio,在某种程度上,这是一个品味问题。有些人会将涉及模板的所有内容放入头文件中。在这种情况下,这就是除了 main 函数之外的所有内容。
    【解决方案3】:

    简短的回答是“你不知道”。

    一种使您想做的事情变得容易的方法(让randomA 对N=1 采取不同的行为)是将const TransitionMatrixTemplate&lt;N, M&gt; randomA() 填充到CRTP 父级中,然后为N=1 部分专门化。

    template<typename D, int N, int M, int K>
    struct Bob_Base {
      static_assert( std::is_base_of< D, Bob_Base<D, N, M, K> >::value, "D must be derived from Bob_Base" );
      D* self() { return static_cast<D*>(this);
      D const* self() const { return static_cast<D*>(this);
      const TransitionMatrixTemplate<N, M> randomA()
      {
        // use self()-> instead of this-> in here to access Bob methods and date
      }
    };
    template<typename D,int M, int K>
    struct Bob_Base< D, 1, M, K > {
      static_assert( std::is_base_of< D, Bob_Base<D, N, M, K> >::value, "D must be derived from Bob_Base" );
      D* self() { return static_cast<D*>(this);
      D const* self() const { return static_cast<D*>(this);
      const TransitionMatrixTemplate<1, M> randomA()
      { /* N=1 version  */ }
    };
    
    template<int N, int M, int K>
    struct Bob : Bob_Base<Bob<N, M, K>, N, M, K>
    {
      //lots of stuff, but no TransitionMatrixTemplate
    }
    

    这只有在N==1 无法编译的代码如果重要(反之亦然)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-04
      • 2010-12-15
      • 2019-09-06
      • 1970-01-01
      • 2017-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多