In my previous post, Recursion, List Manipulation, and Lazy Evaluation, I wrote about the characteristics of functional programming: The story about lazy evaluation in C++ is short. Sorry to say, but I have forgotten templates. The two advanced techniques, CRTP and expression templates, are based on lazy evaluation.
But what does CRTP mean? The acronym CRTP stands for the C++ idiom Curiously Recurring Template Pattern and means a technique in C++ in which a class Derived derives from a class template Base. The key is that Base has Derived as a template argument.
If that is not mind-blowing and how does lazy evaluation kick in? At first lazy evaluation.
As lazy as possible
The key observation for understanding the CRTP idiom is that the instantiation of a method of a class template happens only when needed. Proof?
Although the method func2 (line 8) of the class, Lazy is only declared but not defined; the compiler accepts the program. Because I don’t call func2, I need no definition.
That is precisely the property that the CRTP uses because the definition of a method of class templates is only needed if called. The method’s declaration is sufficient for the instantiation of the base class. Therefore, you can implement static polymorphism.
Static polymorphism is quite similar to dynamic polymorphism. But contrary to dynamic polymorphism with virtual methods, the dispatch of the method calls will occur at compile time. Now, we are at the center of the CRTP idiom.
I use static polymorphism in the function template execution (lines 29 – 32). I invoke on each argument base the method base.interface. The method Base::interface in lines 7 – 9 is the critical point of the CRTP idiom. The methods dispatch to the implementation of the derived class: static_cast<Derived*>(this)->implementation(). That is possible because the method will be instantiated when called. At this point in time, the derived classes Derived1, Derived2, and Derived3 are fully defined. Therefore, the method Base::interface can use the details of its derived classes. Fascinating is the method Base::implementation (lines 10 – 12). This method plays the role of a default implementation for the static polymorphism for the class Derived3 (line 27).
Here is the output of the program.
Admittedly, the only purpose of the example was to present the mechanic behind the static polymorphism. A convincing example is still missing. Here we are.
Mixins with CRTP
Mixins are a popular concept in mixing classes in new code. Therefore, it’s an often-used technique in Python to change the behavior of a class by using multiple inheritances. Contrary to C++, it is legal in Python to have more than one method definition in a class hierarchy. Python uses that method that is first in the Method Resolution Order (MRO).
You can implement mixins in C++ by using CRTP. A prominent example is the class std::enable_shared_from_this. Using this class, you can create objects that return a std::shared_ptr to themselves. You have to derive your class MySharedClass public from std::enable_shared_from_this. Now, your class MySharedClass has a method shared_from_this for creating std::shared_ptr to its objects. You can read the details about std::enable_shared_from_this in my post Specialities of std::shared_ptr.
An additional typical use-case for mixins is a class that you want to extend with the capability that their instances support the comparison of equality and inequality.
I have implemented for the classes Apple and Man the smaller operator (lines 28 and 37). For further reasoning, I will only use the class Man for simplicity. The class Man is publicly derived (lines 32 – 35) from Equality<Man>. I have implemented for classes of the kind Equality<Derived> the equality (lines 9 – 14) and the inequality operator (lines 16 – 21). The inequality operator uses the equality operator (line 20). The equality operator uses the fact that the smaller operator is implemented for Derived (line 13). The equality operator and inequality operator convert its operands: Derived const&: Derived const& d1 = static_cast<Derived const&>(op1).
Now, I can compare Apple and Man for equality and inequality.
In addition to CRTP, expression templates are also based on lazy evaluation. Expression templates are “structures representing a computation at compile time, which are evaluated only as needed to produce efficient code for the entire computation” (https://en.wikipedia.org/wiki/Expression_templates). As needed, that is the point of lazy evaluation; therefore, expression templates are the topic of my next post.
Thanks a lot to my Patreon Supporters: Matt Braun, Roman Postanciuc, Tobias Zindl, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Jozo Leko, John Breland, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Robert Blanch, Truels Wissneth, Kris Kafka, Mario Luoni, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, Peter Ware, Daniel Hufschläger, Alessandro Pezzato, Bob Perry, Satish Vangipuram, Andi Ireland, Richard Ohnemus, Michael Dunsky, Leo Goodstadt, John Wiederhirn, Yacob Cohen-Arazi, Florian Tischler, Robin Furness, Michael Young, Holger Detering, Bernd Mühlhaus, Matthieu Bolt, Stephen Kelley, Kyle Dean, Tusar Palauri, Dmitry Farberov, Juan Dent, George Liao, Daniel Ceperley, Jon T Hess, Stephen Totten, Wolfgang Fütterer, Matthias Grün, Phillip Diekmann, Ben Atakora, Ann Shatoff, Rob North, Bhavith C Achar, Marco Parri Empoli, moon, and Philipp Lenk.
Thanks, in particular, to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, Rusty Fleming, John Nebel, Mipko, Alicja Kaminska, Slavko Radman, and David Poole.
|My special thanks to Embarcadero|
|My special thanks to PVS-Studio|
|My special thanks to Tipi.build|
|My special thanks to Take Up Code|
|My special thanks to SHAVEDYAKS|
I’m happy to give online seminars or face-to-face seminars worldwide. Please call me if you have any questions.
- Embedded Programmierung mit modernem C++ 12.12.2023 – 14.12.2023 (Präsenzschulung, Termingarantie)
Standard Seminars (English/German)
Here is a compilation of my standard seminars. These seminars are only meant to give you a first orientation.
- C++ – The Core Language
- C++ – The Standard Library
- C++ – Compact
- C++11 and C++14
- Concurrency with Modern C++
- Design Pattern and Architectural Pattern with C++
- Embedded Programming with Modern C++
- Generic Programming (Templates) with C++
- Clean Code with Modern C++
- Phone: +49 7472 917441
- Mobil:: +49 176 5506 5086
- Mail: schulung@ModernesCpp.de
- German Seminar Page: www.ModernesCpp.de
- Mentoring Page: www.ModernesCpp.org
Modernes C++ Mentoring,