c++ - Derive class A from a template class Base<A> so that Base<A> can use A::B? -
template <typename t> class base { private: typename t::b c; }; class : public base<a> { public: class b; };
is possible? vc++ 2013 says b not member of a.
i go (live example):
template<typename t> struct impl; template<typename t> struct nested; template <typename t> class base { private: typename nested<t>::type c; }; struct a; template<> struct impl<a> { class b { }; }; template<> struct nested<a> { using type = typename impl<a>::b; }; struct : base<a>, impl<a> { //... };
here, class impl
contains part of a
not depend on base
, is, nested class b
. hence a
derives both base<a>
, impl<a>
.
class nested
contains alias specifying type of above nested class. base
reads type nested
, defines data member, of type.
we need declare a
before specialize impl
, nested
it. , need specializations before defining a
because @ point base<a>
instantiated, , requires nested<a>
complete, in turn requires impl<a>
complete.
the main difference philip's answer responsibilities more separated:
base
not derive anything, has less chance of being polluted. change type of data membertypename nested<t>::type
, that's it.nested
pure type trait. defines aliastype
, that's it.impl
implementation class. contains definition of nested classb
or possibly else not depend onbase
.
by way, stroustrup's 4th edition of the c++ programming language has following code on page 771:
template<typename n> struct node_base : n::balance_type { }; template<typename val, typename balance> struct search_node : node_base<search_node<val, balance> > { using balance_type = balance; };
which has same problem.
Comments
Post a Comment