次のように、特定の型と可変個引数型に依存するテンプレートクラスを記述します。
template<typename ConstType,typename...Inputs>
class ConstantTensor;
次に、別のクラスを作成します。これは通常、このように定義されwrong_type
ます(必要なタイプを想定しますが、次の特殊化とは異なります)。
template<typename T>
class X{
public:
using type=wrong_type;
}
そして、私もこの種の専門分野を持っています:
template<typename ConstType,typename...Inputs>
class X< ConstantTensor< ConstType ,Inputs...>>
{
public:
using type=right_type;
}
私の問題は、型を定義してConstantTensor<ConstType,double>
から使用したいX<ConstantTensor<ConstType,double>>::type
場合、特殊化ではなく一般的なケースが呼び出されることです。だから私はのwrong_type
代わりに取得しますright_type
。double
タイプに対処する必要があると思います...この問題を解決する理由と方法を教えてください。前もって感謝します。
編集:ここにコードのスニペットがあります、私はそれがうまくいくことを願っています:
class Scalar
{};
template<typename ConstType,typename...Inputs>
class ConstantTensor
{
public:
constexpr ConstantTensor(const Inputs&...inputs)
{}
};
template<typename ConstType,typename...Inputs>
constexpr auto Constant(const Inputs&...inputs)
{return ConstantTensor<ConstType,Inputs...>(inputs...);}
template<typename T>
class X{
public:
using type=int;
};
template<typename ConstType,typename...Inputs>
class X<ConstantTensor<ConstType,Inputs...>>{
public:
using type=char;
};
int main()
{
constexpr auto delta=Constant<Scalar>(2.0);
using type= X<decltype(delta)>::type; // this is int not char
}
問題はそれです
constexpr auto delta=Constant<Scalar>(2.0);
あるconstexpr
変数は、だからそれもconst
です。
そうでdecltype(delta)
はありませんConstantTensor<Scalar>
が、ConstantTensor<Scalar> const
です。
const
部分的な特殊化宣言の追加を確認できます
template<typename ConstType,typename...Inputs>
class X<ConstantTensor<ConstType,Inputs...> const>{ // <-- added const
public:
using type=char;
};
今、あなたはそれが得るtype
ですchar
。
-編集-
OPは尋ねます
コードを複製せずに、constとnon constの両方のケースに対処するための短くてエレガントな方法はありますか?
エレガントかどうかはわかりませんが、私には十分に短いように思われます。次の部分的な特殊化を追加して、一種の自己継承を使用できます。
template <typename T>
class X<T const> : public X<T>
{ };
したがってX<ConstantTensor<Scalar> const>
、から継承しX<ConstantTensor<Scalar>>
ます。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加