crtp

C++: CRTP to avoid dynamic polymorphism

How can I use CRTP in C++ to avoid the overhead of virtual member functions? ...

invalid use of incomplete type

I'm trying to use a typedef from a subclass in my project, I've isolated my problem in the example below. Does anyone know where I'm going wrong? template<typename Subclass> class A { public: //Why doesn't it like this? void action(typename Subclass::mytype var) { (static_cast<Subclass*>(this))->do_action(var); ...

C++ CRTP issues, MSVC error C2039

MSVC 2008 won't compile this code: template <class Derived> struct B { typename Derived::type t; }; struct D : B<D> { typedef int type; }; void main() { D d; } The error I get is "error C2039: 'type' : is not a member of 'D'". Any ideas? ...

How to partially specialize a class template for all derived types?

I want to partially specialize an existing template that I cannot change (std::tr1::hash) for a base class and all derived classes. The reason is that I'm using the curiously-recurring template pattern for polymorphism, and the hash function is implemented in the CRTP base class. If I only want to partially specialize for a the CRTP base...

enums and generic methods in java

Hi all, I still have trouble with some corner cases in the java generics system. I have this method (I'm only interested in the signature) : interface Extractor<RETURN_TYPE> { public <U extends Enum<U>> RETURN_TYPE extractEnum(final Class<U> enumType); } (think about an interface whose implementations sometimes extracts an En...

Inherit from a template parameter and upcasting back in c++

Hello, I have tried to use this code in VS2008 (and may have included too much context in the sample...): class Base { public: void Prepare() { Init(); CreateSelectStatement(); // then open a recordset } void GetNext() { /* retrieve next record */ } private: virtual void Init() = 0; virtu...

Just-In-Time Derivation

There's a less common C++ idiom that I've used to good effect a few times in the past. I just can't seem to remember if it has a generally used name to describe it. It's somewhat related to mixins, CRTP and type-erasure, but is not specifically any of those things. The problem is found when you want to add some implementation to a clas...

Dependency on Derived class constructor problem

Hello Group, I am working on a legacy framework. Lets say 'A' is the base-class and 'B' is the derived class. Both the classes do some critical framework initialization. FWIW, it uses ACE library heavily. I have a situation wherein; an instance of 'B' is created. But the ctor of 'A' depends on some initialization that can only be perf...

C++ Compiler error with CRTP

I have the following class hierarchy: template <typename T> class base { public: void f() {} }; class class_a : public base<class_a> {}; class class_b : public base<class_b>, public class_a { using base<class_b>::f; }; int main() { class_b b; b.f(); return 0; } Comeu and Intel C++ v11 claim all is...

Inheritance with CRTP

I have these 3 classes. class A { public: virtual void Func() = 0; }; template<class T> class B : public A { public: void Func() { cout << "In B" << endl; static_cast<T*>(this)->Func(); } }; class C : public B<C> { public: voi...

Mixins, variadic templates, and CRTP in C++

Here's the scenario: I'd like to have a host class that can have a variable number of mixins (not too hard with variadic templates--see for example http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.103.144). However, I'd also like the mixins to be parameterized by the host class, so that they can refer to its public types (using th...

Mixing policy-based design with CRTP in C++

I'm attempting to write a policy-based host class (i.e., a class that inherits from its template class), with a twist, where the policy class is also templated by the host class, so that it can access its types. One example where this might be useful is where a policy (used like a mixin, really), augments the host class with a polymorphi...

How to write curiously recurring templates with more than 2 layers of inheritance?

All the material I've read on Curiously Recurring Template Pattern seems to one layer of inheritance, ie Base and Derived : Base<Derived>. What if I want to take it one step further? #include <iostream> using std::cout; template<typename LowestDerivedClass> class A { public: LowestDerivedClass& get() { return *static_cast<LowestD...

C++ CRTP(template pattern) question

following piece of code does not compile, the problem is in T::rank not be inaccessible (I think) or uninitialized in parent template. Can you tell me exactly what the problem is? is passing rank explicitly the only way? or is there a way to query tensor class directly? Thank you #include <boost/utility/enable_if.hpp> template<cla...

Templated derived class in CRTP (Curiously Recurring Template Pattern)

Hi, I have a use of the CRTP that doesn't compile with g++ 4.2.1, perhaps because the derived class is itself a template? Does anyone know why this doesn't work or, better yet, how to make it work? Sample code and the compiler error are below. Source: foo.C #include <iostream> using namespace std; template<typename X, typename D> str...

Having trouble storing a CRTP based class in a vector

Hi, Im not sure if this can be done, im just delving into templates so perhaps my understanding is a bit wrong. I have a Platoon of soldiers, the platoon inherits from a formation to pick up the formations properties, but because i could have as many formations as i can think of I chose to use the CRTP to create the formations, hoping ...

Using the "curiously recurring template pattern" in a multi-file program

Hello everyone, I'm a pretty novice (C++) programmer and have just discovered the CRTP for keeping count of objects belonging to a particular class. I implemented it like this: template <typename T> struct Counter { Counter(); virtual ~Counter(); static int count; }; template <typename T> Counter<T>::Counter() { ++...

CRTP & multilevel inheritance

I have a class hierarchy and want to get rid of virtual method calls overhead using CRTP pattern. How to do this for my example classes, is it doable ? class A { public: virtual ~A(); virtual void foo(); }; class B : public A { public: virtual ~B(); virtual void foo(); }; class C : public B { public: virtual ~C(); ...

CRTP and templated expressions

In a complex library that uses templated expressions, and the Curiously Recursive Template Pattern (CRTP), I need some overloaded operators to be specialized with base classes, but operations involving their derived classes are not finding the base-class specialization. Ie: if an operator is defined for BaseA< T > + BaseA< T >, then c...

Would using a virtual destructor make non-virtual functions do v-table lookups?

Just what the topic asks. Also want to know why non of the usual examples of CRTP do not mention a virtual dtor. EDIT: Guys, Please post about the CRTP prob as well, thanks. ...