CRTP vs. Direct Derived Function Implementation

I am trying to better understand CRTP. Until now, I understand that it allows you to write functions such as the following.

template <class T> void foo(Base<T> x ) { x.do_stuff() } 

Now, depending on the actual compile-time object x that is passed to the foo() function, it will do different things.

However, I could get the Derived class from Base and mask / shadow it with do_stuff() not virtual, but overridden by Derived::do_stuff . Therefore, when exactly is it correct to use CRTP, and the simplest non-trivial example that shows the advantage of CRTP with respect to shading / masking.

+4
source share
1 answer

The CRTP point should be able to get the type of the derived object without virtuality. If you do

 struct B { void foo() const; } struct D : B { void foo() const; } void bar(const B& x) { x.foo(); } 

then bar calls B::foo , not D::foo when passing the object D , since foo not a virtual function. If you want D::foo called, you need either virtual functions or CRTP.

The simplest form of CRTP:

 template <typename> struct B { void foo() const; } struct D : B<D> { void foo() const; } template <typename T> void bar(const B<T>& x) { static_cast<const T&>(x).foo(); } 

this calls D::foo() when you go to the bar a D object.

An alternative CRTP trick, which, however, forces D provide an implementation for foo ,

 template <typename T> struct B { void foo() const { static_cast<const T*>(this)->foo_impl(); } // default implementation if needed // void foo_impl() const { ... } }; struct D : B<D> { void foo_impl() const { ... } }; template <typename T> void bar(const B<T>& x) { x.foo(); } 

but you still need the template parameter for B (so that foo sent correctly) and therefore the bar template.

Also, if you are not using CRTP, you better have a virtual destructor that can add unwanted overhead for lightweight classes that need to be fully integrated. With CRTP, you simply write a protected destructor (private destructor + friend T in C ++ 0x).

+5
source

Source: https://habr.com/ru/post/1369616/


All Articles