テンプレートメソッドの戻り値として、別のテンプレートのパラメーターとして別のテンプレートクラスで定義されているテンプレートクラスを使用する必要があります。複雑に聞こえるかもしれませんが、以下のコードでよりよく説明されています。問題は、コードをコンパイルできないことです。次のエラーで終了します。
type/value mismatch at argument 2 in template parameter list for 'template<class T, template<class> class Policy> class Result'
expected a class template, got 'CDummy<T2>::Policy2'
しかし、特定のクラスがニーズを満たしていると確信しています。問題は、それを使用するメソッドもテンプレートであるため、コンパイラーが正確に何であるかを知らないことCDummy<T2>::Policy2
です。Policy2
がテンプレートではなく通常のクラスである場合、またはその引数を入力できる場合は、コンパイラに心配しないように指示する which を使用しtypename
ますが、これをテンプレートでどのように行うことができますか?
// I cannot change this interface - it's given by a library
template <class T, template <class> class Policy>
class Result : public Policy<T>
{
T data;
};
template <class T>
class Policy1
{
};
// I use this for allowing Policy2 to change behaviour according Dummy
// while it keeps template interface for class above
template <class Dummy>
class CDummy
{
public:
template <class T>
class Policy2 : public Policy1<T>
{
};
};
// Both variables are created ok
Result<int, Policy1 > var1;
Result<int, CDummy<float>::Policy2 > var2;
// This is ok, too
template <class T>
Result<T, Policy1 > calc1()
{
return Result<int, Policy1>();
}
// But this ends with the error:
// type/value mismatch at argument 2 in template parameter list for 'template<class T, template<class> class Policy> class Result'
// expected a class template, got 'CDummy<T2>::Policy2'
template <class T1, class T2>
Result<T1, CDummy<T2>::Policy2 > calc2() // <-- Here is the generated error
{
typedef typename DummyTypedef CDummy<T2>;
return Result<T1, DummyTypedef::Policy2>();
}
ノート:
- GNU/Linux Ubuntu 13.04 で gcc 4.7.3 32 ビットを使用しています。32ビット。
- さまざまな理由で、C++11 標準を (まだ) 使用できないため、テンプレートの typedef を使用できません