Thread: Is This Legal C++? [CRTP]

  1. #1
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970

    Is This Legal C++? [CRTP]

    Is this usage of the Curiously Recurring Template Pattern legal?

    Can I call a derived member function from a base class constructor like this?

    Code:
    #include <cstdio>
    
    using std::printf;
    
    template <typename T>
    class Base {
    public:
      Base() { static_cast<T*>(this)->echo(); }
      void echo() { printf("Base"); }
    };
    
    class Derived: public Base<Derived> {
    public:
      void echo() { printf("Derived"); }
    };
    
    int main()
    {
      Derived derived;
    }
    My output is "Derived".

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Yes. The object is fully constructed once inside the constructor body. You can then up or down cast this, as long as it's done in a safe manner.

    gg

  3. #3
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    Thanks for responding Codeplug.

    My assumption is that the key reason this technique works, is because Base knows Derived exists, and just uses a Derived pointer, and not a concrete Derived object.

    So without templates, I might have tried something like this?

    Code:
    #include <cstdio>
    
    class Base{
    public:
      Base();
      void echo() { printf( "Echo" ); }
    };
    
    class Derived : public Base{
    public:
      void echo(){ printf("Derived"); }
    };
    
    Base::Base(){
      (static_cast<Derived *>(this))->echo();
    }
    
    int main() {
      Derived derived;
    }

  4. #4
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    I've emailed Bjarne Stroustrup. He says my template version is totally illegal, it may work on most implementations, but it's not guaranteed.

    Details can be found here.

  5. #5
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    it is illegal because you are using a derived object before it is fully constructed. Sure it may work especially if derived adds no data members but it is definately illegal.
    make the fuction virtual. Notice the difference. Now explain why base::echo() would be called if the function was virtual.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  6. #6
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    make the fuction virtual. Notice the difference. Now explain why base::echo() would be called if the function was virtual.
    Because derived is not constructed yet.

    I'm aware of this, that's why I tried to cheat using templates instead of virtual.

  7. #7
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    then you answered your own question
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  8. #8
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    My question was about templates, not virtual functions.

    And my reason for asking: the template code works on the compiler I tested.

  9. #9
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> The object is fully constructed once inside the constructor body.
    I thought objects were constructed in reverse order on Saturdays.....oh well.

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What is legal and what is illegal?
    By dlwlsdn in forum C Programming
    Replies: 3
    Last Post: 11-14-2008, 12:48 PM
  2. Replies: 1
    Last Post: 04-03-2008, 01:17 AM
  3. Legal Question: May I put my name(e-mail link) on the interface of a product
    By zahid in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 06-17-2003, 11:34 PM
  4. is this legal?
    By Shadow12345 in forum C++ Programming
    Replies: 6
    Last Post: 04-16-2002, 12:44 PM