次のコードは問題なく動作します (これは私の他の問題の単純化されたバージョンであり、型が長く、深く、テンプレートが多くなっています):
template<class C>
struct Base
{};
template<class C>
struct Derived : public Base<C>
{
Derived() : Base<C>()
{}
};
しかし、基本クラスの完全な型を「書き込む」ことなく、どうすれば基本クラスのコンストラクターを呼び出すことができるでしょうか? たとえば、次のようなことを試しました。
template<class C>
struct Base
{
typedef Base base_type;
};
template<class C>
struct Derived : public Base<C>
{
Derived() : base_type() {}
};
int main()
{
Derived<void> b;
}
しかし、「base_type」は認識されません。gcc がスローするメッセージは次のとおりです。
test3.cpp: In constructor 'Derived<C>::Derived()':
test3.cpp:100:17: error: class 'Derived<C>' does not have any field
named 'base_type'
それを解決するにはBase<C>::base_type
、コンストラクターに記述する必要がありますが、これはbase_type
それ自体の存在が無関係になります。
私の節約術は無理ですか?
そして、なぜbase_type
コンストラクターで見つからないのに、これはうまくいくのでしょうか?
int main()
{
Derived<void>::base_type b;
}
編集: @Jack Aidleyのコメントにより、単純なエイリアスを持つ基本クラスの型を取得するために私が見つけた最良の形式は次のとおりです。
template<typename C> struct Base {};
template<typename C, typename Base>
struct Derived_impl : public Base
{
Derived_impl() : Base()
{}
};
template<typename C>
using Derived = Derived_impl<C, Base<C> >;
int main()
{
Derived<void> b;
}