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, now we are at the center of lazy evaluation and the center of this post.
What problem do expression templates solve? Thanks to expression templates, you can eliminate superfluous temporary objects in expressions. What do I mean by superfluous temporary objects? My implementation of the class MyVector.
A first naive approach
MyVector is a simple wrapper for a std::vector<T>. The wrapper has two constructors (lines 12 and 15), knows its length (lines 18 – 20), and supports the reading (lines 23 – 25) and writing (lines 27 – 29) index access.
Thanks to the overloaded + operator (lines 34 – 41), the overloaded * operator (lines 44 – 51), and the overloaded output operator (lines 54 – 62), the objects x, y, and result feel like numbers.
Why is this implementation naive? The answer is in the expression result= x+x + y*y. Three temporary objects are needed to evaluate the expression to hold the result of each arithmetic sub-expression.
How can I get rid of the temporaries? The idea is simple. Instead of performing the vector operations greedy, I lazily create the expression tree for result[i] at compile time.
There are no temporaries needed for the expression result[i]= x[i]+x[i] + y[i]*y[i]. The assignment triggers the evaluation. Sadly, even in this simple usage, the code is not so easy to digest.
The key difference between the first naive implementation and this implementation with expression templates is that the overloaded + and + operators return in the case of the expression tree proxy objects. These proxies represent the expression tree (lines 94 and 100). The expression tree is only created but not evaluated. Lazy, of course. The assignment operator (lines 22 – 27) triggers the evaluation of the expression tree that needs no temporaries.
The result is the same.
Suppose you were not able to follow my explanation, no problem. The assembler code of the program vectorArithmeticExpressionTemplates.cpp shows the magic.
Under the hood
Thanks to the compiler explorer on godbolt.org, it’s quite easy to have the assembler instructions.
The expression tree in line 60 is not so beautiful. But with a sharp eye, you can see the structure. For simplicity reasons, I ignored the std::allocator in my graphic.
With the next post, I will start the rework of my blog. That means I will rework old posts and write new posts to complete my stories. I will start in the next post with the multithreading features of C++17 and C++20. Here is an overview of all my posts.
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,