Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3393306
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:02:06+00:00 2026-05-18T04:02:06+00:00

Without referring to a book, can anyone please provide a good explanation for CRTP

  • 0

Without referring to a book, can anyone please provide a good explanation for CRTP (curiously recurring template pattern) with a code example?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T04:02:07+00:00Added an answer on May 18, 2026 at 4:02 am

    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> {...};
    

    It is curiously 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 could make a generic singleton class (simplified version) like this

    #include <iostream>
    
    template <class T>
    class Singleton
    {
    public:
         static T* GetInstance() {
             if ( p == nullptr ) p = new T();
             return p;
         }
    protected:
         Singleton() = default;
    
         Singleton(Singleton const &) = delete;
         Singleton &operator=(const Singleton &) = delete;
    
    private:
         static T *p;
    };
    template <class T>
    T *Singleton<T>::p= nullptr;
    
    

    Now, in order to make an arbitrary class A a singleton you should do this

    class A : public Singleton<A> 
    { 
    friend Singleton;
    private:
        A() = default;
    };
    A *a0= A::GetInstance();
    

    Howevry, CRTP is not necessary in this case, see as follow:

    class C 
    { 
    friend Singleton<C>; 
    private: C() = default;
    };
    C *c1= Singleton<C>::GetInstance();
    

    So you see? The singleton template assumes that its specialization for any type X will be inherited from singleton<X> and thus will have all its (public, protected) members accessible, including the GetInstance! 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 exercise!

    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 could 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, isn't it?
        Derived const& d2 = static_cast<Derived const&>(op2); 
        return !(d1 < d2) && !(d2 < d1);//assuming derived has operator <
    }
    

    or implement within the template scope without casting

    template<class T>
    class Equality
    {
        friend bool operator == (const T& op1, const T& op2)
        { 
            return !(op1 < op2) && !(op2 < op1); 
        }
    };
    

    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 could seem that you would write less if you just wrote operator == for Apple, but imagine that the Equality template would provide not only == but >, >=, <= etc. And you could use these definitions for multiple classes, reusing the code!

    CRTP is a wonderful thing 🙂 HTH

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I loop through this json data without referring to the data items
Can anyone explain what the jquery documentation is exactly referring to with this statement:
Without having to write VBA code, is there a way to maybe write a
Without going into whether this is a good or bad idea: Is it possible
How to select all records,that may contain specific value that is known, without referring
How do you create a Web Popup Window without referring to a new html
Selecting constants without referring to a table is perfectly legal in an SQL statement:
I need grab the path of a referring URL, which can be served from
Referring to this Python List Comprehension Vs. Map question, can someone explain why List
Without using any other jquery plugin I'm looking to continuously flash/change a div's border

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.