c++ - Subclass B Inherits From a Template Class A<B> -
this question has reply here:
what curiously recurring template pattern (crtp)? 3 answersi stumbled upon code looks , can't wrap head around it:
template<typename t> class { } class b: a<b> { } so general questions are:
why not give compile error? how can classb inherit template class a<b>, if b hasn't been defined yet? when construction ever necessary?
one of features: template pattern can help avoid vtable usage. called "static polymorphism" - http://en.m.wikipedia.org/wiki/curiously_recurring_template_pattern
suppose have code structure:
class item { public: virtual void func() = 0; } class : public item { // … } class b : public item { // … } item *item = new a(); item->func(); it can replaced this:
template<typename t> class item { public: void func() { t::func(); } } class : public item<a> { // … } class b : public item<b> { // … } item<a> *item = new a(); item->func(); this way can avoid virtual function call. can done performance improvement...
c++ templates inheritance metaprogramming
No comments:
Post a Comment