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

  • Home
  • SEARCH
  • 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 780037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:00:56+00:00 2026-05-14T20:00:56+00:00

All the material I’ve read on Curiously Recurring Template Pattern seems to one layer

  • 0

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<LowestDerivedClass*>(this); }
    void print() { cout << "A\n"; }
};
template<typename LowestDerivedClass> class B : public A<LowestDerivedClass> {
    public: void print() { cout << "B\n"; }
};
class C : public B<C> {
    public: void print() { cout << "C\n"; }
};

int main()
{
    C c;
    c.get().print();

//  B b;             // Intentionally bad syntax, 
//  b.get().print(); // to demonstrate what I'm trying to accomplish

    return 0;
}

How can I rewrite this code to compile without errors and display

C
B

Using c.get().print() and b.get().print() ?

Motivation: Suppose I have three classes,

class GuiElement { /* ... */ };
class Window : public GuiElement { /* ... */ };
class AlertBox : public Window { /* ... */ };

Each class takes 6 or so parameters in their constructor, many of which are optional and have reasonable default values. To avoid the tedium of optional parameters, the best solution is to use the Named Parameter Idiom.

A fundamental problem with this idiom is that the functions of the parameter class have to return the object they’re called on, yet some parameters are given to GuiElement, some to Window, and some to AlertBox. You need a way to write this:

AlertBox box = AlertBoxOptions()
    .GuiElementParameter(1)
    .WindowParameter(2)
    .AlertBoxParameter(3)
    .create();

Yet this would probably fail because, for example, GuiElementParameter(int) probably returns GuiElementOptions&, which doesn’t have a WindowParameter(int) function.

This has been asked before, and the solution seems to be some flavor of the Curiously Recurring Template Pattern. The particular flavor I use is here.

It’s a lot of code to write every time I create a new Gui Element though. I’ve been looking for ways to simplify it. A primary cause of complexity is the fact that I’m using CRTP to solve the Named-Parameter-Idiom problem, but I have three layers not two (GuiElement, Window, and AlertBox) and my current workaround quadruples the number of classes I have. (!) For example, Window, WindowOptions, WindowBuilderT, and WindowBuilder.

That brings me to my question, wherein I’m essentially looking for a more elegant way to use CRTP on long chains of inheritance, such as GuiElement, Window, and Alertbox.

  • 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-14T20:00:56+00:00Added an answer on May 14, 2026 at 8:00 pm

    Here is what I’ve settled on, using a variation on CRTP’s to solve the problem presented in my motivation example. Probably best read starting at the bottom and scrolling up..

    #include "boost/smart_ptr.hpp"
    using namespace boost;
    
    // *** First, the groundwork....
    //     throw this code in a deep, dark place and never look at it again
    //
    //     (scroll down for usage example)
    
    #define DefineBuilder(TYPE, BASE_TYPE) \
        template<typename TargetType, typename ReturnType> \
        class TemplatedBuilder<TYPE, TargetType, ReturnType> : public TemplatedBuilder<BASE_TYPE, TargetType, ReturnType> \
        { \
        protected: \
            TemplatedBuilder() {} \
        public: \
            Returns<ReturnType>::me; \
            Builds<TargetType>::options; \
    
    template<typename TargetType>
    class Builds
    {
    public:
        shared_ptr<TargetType> create() {
            shared_ptr<TargetType> target(new TargetType(options));
            return target;
        }
    
    protected:
        Builds() {}
        typename TargetType::Options options;
    };
    
    template<typename ReturnType>
    class Returns
    {
    protected:
        Returns() {}
        ReturnType& me() { return *static_cast<ReturnType*>(this); }
    };
    
    template<typename Tag, typename TargetType, typename ReturnType> class TemplatedBuilder;
    template<typename TargetType> class Builder : public TemplatedBuilder<TargetType, TargetType, Builder<TargetType> > {};
    
    struct InheritsNothing {};
    template<typename TargetType, typename ReturnType>
    class TemplatedBuilder<InheritsNothing, TargetType, ReturnType> : public Builds<TargetType>, public Returns<ReturnType>
    {
    protected:
        TemplatedBuilder() {}
    };
    
    // *** preparation for multiple layer CRTP example *** //
    //     (keep scrolling...)
    
    class A            
    { 
    public: 
        struct Options { int a1; char a2; }; 
    
    protected:
        A(Options& o) : a1(o.a1), a2(o.a2) {}
        friend class Builds<A>;
    
        int a1; char a2; 
    };
    
    class B : public A 
    { 
    public: 
        struct Options : public A::Options { int b1; char b2; }; 
    
    protected:
        B(Options& o) : A(o), b1(o.b1), b2(o.b2) {}
        friend class Builds<B>;
    
        int b1; char b2; 
    };
    
    class C : public B 
    { 
    
    public: 
        struct Options : public B::Options { int c1; char c2; };
    
    private:
        C(Options& o) : B(o), c1(o.c1), c2(o.c2) {}
        friend class Builds<C>;
    
        int c1; char c2; 
    };
    
    
    // *** many layer CRTP example *** //
    
    DefineBuilder(A, InheritsNothing)
        ReturnType& a1(int i) { options.a1 = i; return me(); }
        ReturnType& a2(char c) { options.a2 = c; return me(); }
    };
    
    DefineBuilder(B, A)
        ReturnType& b1(int i) { options.b1 = i; return me(); }
        ReturnType& b2(char c) { options.b2 = c; return me(); }
    };
    
    DefineBuilder(C, B)
        ReturnType& c1(int i) { options.c1 = i; return me(); }
        ReturnType& c2(char c) { options.c2 = c; return me(); }
    };
    
    // note that I could go on forever like this, 
    // i.e. with DefineBuilder(D, C), and so on.
    //
    // ReturnType will always be the first parameter passed to DefineBuilder.
    // ie, in 'DefineBuilder(C, B)', ReturnType will be C.
    
    // *** and finally, using many layer CRTP builders to construct objects ***/
    
    int main()
    {
        shared_ptr<A> a = Builder<A>().a1(1).a2('x').create();
        shared_ptr<B> b = Builder<B>().a1(1).b1(2).a2('x').b2('y').create();
        shared_ptr<B> c = Builder<C>().c2('z').a1(1).b1(2).a2('x').c1(3).b2('y').create(); 
        // (note: any order works)
    
        return 0;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.