少しバリエーションのあるCRTPを使おうとしています。派生クラステンプレートが1つあり、これを複数の基本クラスに適用したいと考えています。しかし、これは不可能であるか、構文を正しく理解できないかのどちらかです。次のコードはコンパイルされませんが、うまくいけば、私が達成したいことを示しています。
template <class Derived> struct BaseCats { /* ... */ };
template <class Derived> struct BaseDogs { /* ... */ };
// ....
template <class Derived> struct BaseN { /* ... */ };
template <template <class> class Base>
struct Wrapper
:
Base<Wrapper> // compile error - Wrapper is not a complete type
{
Wrapper(int n)
{
// I do not want to rewrite or forward this
// constructor or Wrapper's operators
}
};
typedef Wrapper<BaseCats> Cats;
typedef Wrapper<BaseDogs> Dogs;
// ...
typedef Wrapper<BaseN> MyTypeN;
これはできますか?
編集:
私はここで何を達成しようとしていますか?
上記のコードの一部の名前を「犬と猫」のメタファーを使用するように変更しました。次のような機能が存在する可能性があります。
void BaseCats<Derived>::print() const
{
std::cout << (static_cast<const Derived *>this)->n << " cats\n";
}
ただしWrapper
、犬と猫の両方に共通のコンストラクターと演算子が含まれます。基本クラスが特殊化されている一種の逆ポリモーフィズム。このようにする理由は、コンストラクターと演算子を専門分野ごとに書き直したり転送したりする必要がないようにするためです。