3

テンプレートメソッドの戻り値として、別のテンプレートのパラメーターとして別のテンプレートクラスで定義されているテンプレートクラスを使用する必要があります。複雑に聞こえるかもしれませんが、以下のコードでよりよく説明されています。問題は、コードをコンパイルできないことです。次のエラーで終了します。

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 を使用できません
4

1 に答える 1

4

その名前CDummy<T2>::Policy2はそのコンテキストでは依存名であり、templateキーワードを使用して、それが実際にテンプレートであることをコンパイラーに通知する必要があると思います。

template <class T1, class T2>
Result<T1, CDummy<T2>::template Policy2 > calc2() // <-- Here is the generated error
//                     ^^^^^^^^

さらに、その同じ機能の実装も間違っているようです。typedefsの順序は、元の名前新しい名前CDummy<T2>型であることがわかっています (つまり、 は必要ありませんtypename)。

typedef CDummy<T2> DummyTypedef;

return ステートメントは次のようになります。

return Result<T1, DummyTypedef::template Policy2>();
于 2013-06-25T16:14:43.073 に答える