私は可変テンプレートを試していて、奇妙な問題が発生しました。入れ子クラスの可変テンプレート
template<int v0, int ...Vs>
class Foo
{
public:
template<typename nested = void,
typename = void>
class Reduce
{
public:
static int const value = v0;
};
template<typename nested>
class Reduce<nested,
typename std::enable_if<0<sizeof...(Vs)>::type>
{
public:
static int const value = v0 + Foo<Vs...>::template Reduce<>::value;
};
};
このコードはテンプレートクラスでネストされたテンプレートクラスは完全に専門的なことができないので、私はネストされたダミーのテンプレートパラメータを挿入し、エラーメッセージ
//example code
int s = Foo<0,1,2,3,4,5>::Reduce<>::value;
std::cout << s << std::endl;
error: incomplete type 'F<5>::Reduce<>' used in nested name specifier
をもたらします。実際に私はコンパイラが不完全な型について不平を言う理由を理解していない。
私は
template<int v0, int ...Vs>
class Reduced
{
public:
static int const value = v0 + Reduce<Vs...>::value;
};
template<int v0>
class Reduced<v0>
{
public:
static int const value = v0;
};
template<int v0, int ...Vs>
class Foo
{
public:
class Reduce
{
public:
static int const value = Reduced<v0, Vs...>::value;
};
};
その作業のに可変長のものを変更するが、他の1がない理由を私は知りません。コンパイラがそのエラーを投げている理由は何ですか?手伝ってくれてありがとう。
このアプローチでは、テンプレート引数 'nested'はまったく必要ありません。 さらに、私はFooクラスの特殊化を避けるのが好きです。実際の使用例では、他の多くのものが含まれているため、私は専門化のために再実装したくありません。 – Feuerteufel
@Feuerteufelはい、テンプレート引数はこのアプローチでは余計です。 – 101010
@Feuerteufel私はあなたに合った編集チェックを追加しました。 – 101010