Curiously recurring template Pattern

Source: Internet
Author: User

Transferred from stackoverflow and Wiki

In short, crtp is when a Class A has a base class which is a template specialization for the Class A itself. E. g.

template <class T> class X{...};class A : public X<A> {...};

ItIsCuriously recurring, isn' t it? :)

Now, what does this give you? This actually gives the X template the ability to be a base class for its specializations.

For example, you cocould make a generic Singleton class (simplified version) like this

template <class ActualClass> class Singleton{   public:   static ActualClass& GetInstance()   {      if(p == 0)         p = new ActualClass;      return *p;    }   protected:   static Actualclass* p;   private:   Singleton(){}   Singleton(Singleton const &);   Singleton& operator = (Singleton const &); };template <class T>T* Singleton<T>::p = 0;

Now, in order to make an arbitrary Class A a singleton you shoshould do this

class A: public Singleton<A>{   //Rest of functionality };

So you see? The Singleton template assumes that its specialization for any Type X will be inherited fromsingleton<X>And thus will have all its (public, protected) members accessible, includingGetInstance! There are other useful uses of crtp. for example, if you want to count all instances that currently exist for your class, but want to encapsulate this logic in a separate template (the idea for a concrete class is quite simple-have a static variable, increment in ctors, decrement in dtors ). try to do it as an excercise!

Yet another useful example, for Boost (I am not sure how they have implemented it, But crtp will do too ). imagine you want to provide only operator <for your classes but automatically operator = for them!

You coshould do it like this:

template<class Derived>class Equality{};template <class Derived>bool operator == (Equality<Derived> const& op1, Equality<Derived> const & op2){    Derived const& d1 = static_cast<Derived const&>(op1);//you assume this works         //because you know that the dynamic type will actually be your template parameter.    //wonderful, isnit it?    Derived const& d2 = static_cast<Derived const&>(op2);     return !(d1 < d2) && !(d2 < d1);//assuming derived has operator <}

Now you can use it like this

struct Apple:public Equality<Apple> {    int size;};bool operator < (Apple const & a1, Apple const& a2){    return a1.size < a2.size;}

Now, you haven't provided explicitly operator = for Apple? But you have it! You can write

int main(){    Apple a1;    Apple a2;     a1.size = 10;    a2.size = 10;    if(a1 == a2) //the compiler won‘t complain!     {    }}

This cocould seem that you wocould write less if you just wrote operator = for Apple, but imagine that the capacity ity template wocould provide not only = but>, >=, <= etc. and you cocould use these definitionsMultipleClasses, reusing the code!

Crtp is a wonderful thing :) hth

Stackoverflow address

Wiki address

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.