簡體   English   中英

部分模板專門化更大類的單個方法

[英]Partial template specialization of a single method of a bigger class

我有以下課程;

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

現在我想將方法randomA專門用於N = 1。 我該怎么做?

我嘗試了這個問題: 模板化的一個方法的模板特化 ,但它似乎不適用於部分特化。 這個問題: C ++部分方法專業化似乎更相關,但它建議專門化整個類(在我的情況下這是非常大的)。 是否可以專門化整個班級,但實際上只專注於這一種方法?

我想將方法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
  }

您不能部分專門化功能模板,但可以將工作傳遞給模板。 這是一個完全有效的例子:

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

簡短的回答是“你沒有”。

一種讓你很容易做你想做的事情的方法(對於N=1 randomA行為不同)就是將const TransitionMatrixTemplate<N, M> 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不能編譯(或反之亦然)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM