copro はすでに 2 つの解決策について言及し、Anonymous は 2 つ目の解決策を説明しましたが、最初の解決策を理解するのにかなりの時間がかかりました。おそらく、次のコードは、私のように、まだグーグルで上位にランクされているこのサイトに出くわした人にとって役立つでしょう. 例 (numericT のベクトル/配列/単一要素を dataT として渡し、[] を介して、または直接アクセスする) はもちろんいくらか工夫されていますが、メンバー関数をラップすることで部分的に特殊化することに実際にどのように近づくことができるかを示す必要があります。部分的に専門化されたクラスで。
/* The following circumvents the impossible partial specialization of
a member function
actualClass<dataT,numericalT,1>::access
as well as the non-nonsensical full specialisation of the possibly
very big actualClass. */
//helper:
template <typename dataT, typename numericalT, unsigned int dataDim>
class specialised{
public:
numericalT& access(dataT& x, const unsigned int index){return x[index];}
};
//partial specialisation:
template <typename dataT, typename numericalT>
class specialised<dataT,numericalT,1>{
public:
numericalT& access(dataT& x, const unsigned int index){return x;}
};
//your actual class:
template <typename dataT, typename numericalT, unsigned int dataDim>
class actualClass{
private:
dataT x;
specialised<dataT,numericalT,dataDim> accessor;
public:
//... for(int i=0;i<dataDim;++i) ...accessor.access(x,i) ...
};