Related Searches


Curiously recurring template pattern

The curiously recurring template pattern (CRTP) is a C++ idiom in which a class X derives from a class template instantiation using X itself as template argument. The name of this idiom was coined by Jim Coplien, who had observed it in some of the earliest C++ template code.

General form

// The Curiously Recurring Template Pattern (CRTP) template struct base {

   // ...
}; struct derived : base {
   // ...

Some use cases for this pattern are static polymorphism, and other metaprogramming techniques such as those described by Alexandrescu in Modern C++ Design .

Static polymorphism

Typically, the base class template will take advantage of the fact that member function bodies (definitions) are not instantiated until long after their declarations, and will use members of the derived class within its own member functions, via the use of a static_cast, or simply a cast e.g.:

template struct Base {

   void interface()
       // ...
       // ...

   static void static_func()
       // ...
       // ...
} };

struct Derived : Base {

   void implementation();
   static void static_sub_func();

This technique achieves a similar effect to the use of virtual functions, without the costs (and some flexibility) of dynamic polymorphism. This particular use of the CRTP has been called "simulated dynamic binding" by some. This pattern is used extensively in the Windows ATL and WTL libraries.

To elaborate on the above example, consider a base class with no virtual functions. Whenever the base class calls another member function, it will always call its own base class functions. When we inherit from this class, a derived class, we inherit all the member variables and member functions that weren't overridden (no constructors or destructors, of course, either). If the derived class calls an inherited function that then calls another member function, that function will never call any derived or overridden member functions in the derived class. As a result of this behavior, most C++ programmers define member functions as virtual to avoid this problem.

However, if base class member functions use the CRTP pattern for all member function calls, the overridden functions in the derived class will get selected at compile time. This effectively emulates the virtual function call system at compile time without the costs in size or function call overhead (VTBL structures, and method lookups, multiple-inheritance VTBL machinery) and the slight disadvantage of not being able to do this choice at runtime.

Object counter

The main purpose of an object counter is retrieving statistics of object creation and destruction for a given class. This can be easily solved using CRTP:

template struct counter {


   virtual ~counter()
   static int objects_created;
   static int objects_alive;
}; template int counter::objects_created(0 ); template int counter::objects_alive(0 );

class X : counter {

   // ...

class Y : counter {

   // ...

Each time an object of class X is created, the constructor of counter is called, incrementing both the created and alive count. Each time an object of class X is destroyed, the alive count is decremented. It is important to note that counter and counter are two separate classes and this is why they will keep separate counts of X's and Y's. Also note that counter does not necessarily use the type in this example, except to keep the counts separate.

In other languages

The CRTP makes an appearance in the Java programming language standard library where the Enum class is defined as Enum<T extends Enum<T>>. Java programmers use the CRTP in practice when they write a comparable class: class X implements Comparable<X>.

See also


Search another word or see curiouslyon Dictionary | Thesaurus |Spanish
Copyright © 2014, LLC. All rights reserved.
  • Please Login or Sign Up to use the Recent Searches feature