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 9220237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:18:04+00:00 2026-06-18T03:18:04+00:00

Everywhere I read about CRTP and indeed in the code I write, a CTRP

  • 0

Everywhere I read about CRTP and indeed in the code I write, a CTRP class hierarchy looks something like the following:

template< class T >
class Base
{

public:

    int foo_interface() 
    { 
        return static_cast< T* >(this)->foo_implementation();                        
    }

};

class Derived : public Base< Derived >
{

    friend class Base< Derived >;

    int foo_implementation() 
    { 
        return 5;
    }

};

That is, the name of the interface and implementing method is different. Now, I generally don’t want the implementation methods to be visible from the outside, which necessitates the friend declaration above and in multi-level hierarchies turns out to be a major kludge (even with the trick described here).

Now, I came up with the following:

// Base class
template< class T >
class A
{

public:

    int foo() 
    {             
        std::cout << "I'm in A's foo!\n";
        return static_cast< T * >(this)->foo();            
    }

};

// Deriving class
class B : public A< B >
{

public:

    int foo()
    { 
        std::cout << "I'm in B's foo!\n";
        return 5; 
    }

};

// Deriving class with a nasty surprise...
class C: public A< C >
{

public:

    // ...crap, no foo to be found!

    int bar() 
    {             
        std::cout << "I'm in C's bar!\n";
        return 12; 
    }

};

template< class T >
int call_foo(A< T > & t)
{
    return t.foo();
}

B b;
C c;

Now, call_foo(b) works just like I would expect, calling B’s implementation of foo(). Likewise, call_foo(c) also works as expected (in that it doesn’t… it gets stuck in an infinite loop for obvious reasons). One drawback I can see is that if I forget to implement a method in the deriving class (or misspell something, forget to qualify it as const, whatever…), I get an infinite loop, so it might make bugs of that sort a bit harder to find since they’re not caught at compile-time. Other than that, though, it’s almost as simple as just plain virtual functions, and I don’t have to fight with hiding the implementation methods. It seems easy and elegant, yet nobody seems to be using it… my question is, then, what’s the catch? Why isn’t this approach used? Is hiding the implementing methods simply not a big deal? Or is there some kind of immeasurably sinister evil force lurking in there ready to devour my soul the moment I try this approach on a real project?

  • 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-06-18T03:18:05+00:00Added an answer on June 18, 2026 at 3:18 am

    You pretty much stated the reason: A failure to implement the function results in an infinite loop.

    By separating the interface from the implementation, it allows two things to happen when the derived class doesn’t provide the implementation

    1) If the base class has it’s own implementation, it behaves like a normal virtual function where the base class has a default implementation.
    2) If the base class does not provide an implementation, it fails to compile. Again, this is similar to a pure virtual function.

    Finally, there are some (such as Herb Sutter) who suggest always separating the interface (public function) from the implementation (private function) when using virtual methods. Give http://www.gotw.ca/publications/mill18.htm a read. By performing the separation as part of CRTP, you get the same benefits.

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

Sidebar

Related Questions

Everywhere I read about Mercurial and Git they generally throw in a line or
Everywhere I read about converting time to a user's timezone says that the best
Everywhere I read about the new DLR in .net 4, they say that a
I have read about system calls in Linux, and everywhere description is given regarding
Okay, just about everywhere I read, I read that getters/setters are evil. Now, as
OK, so I've read something like 10 other threads here regarding this infamous issue,
I've read about XSS vulnerability in desktop applications like Skype and ICQ. I wonder
I have read about piconets and scatternets. However, everywhere I've checked I do not
I have read everywhere so many different things about opening the facebook app from
I am a newbie to python,everywhere I read about list methods I see one

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.