The ranges library in C++20 made due to performance reasons a few unique design choices. These choices have consequences: cache issues and constness issues.
Here is a short reminder. In my last post, “The Ranges Libray in C++20: Design Choices“, I presented this possible implementation of
The key observation is that the
begin iterator is cached for subsequent calls. This caching has two interesting consequences:
- Don’t use a view on modified ranges.
- Don’t copy a view.
Or, to put it positively: Use views directly after you have defined them.
There are more design choices you must know.
The member function of a view may cache the position. This implicates the following:
- A function taking an arbitrary view should take its view by universal reference.
- Reading two views concurrently may be a data race.
Let’s discuss the first consequence.
Take Arbitrary Views by Universal Reference
printElements takes its view by universal reference.
printElements takes its argument by universal reference. Taking it by lvalue reference or by value is, in general, no option.
Taking the argument by
const lvalue reference may not work because the implicit
begin call on the view could modify it. On the contrary, a non-
const lvalue reference cannot handle rvalues.
Taking the argument by value may invalidate the cache.
Concurrent Reading of Views may be a Data Race
The following program exemplifies the concurrency issue with views:
I iterate in the program
dataRaceRanges.cpp concurrently two times through a view in a non-modifying way. First, I iterate in the
std::jthread thr1 and second in the main function. This is a data race because both iterations implicitly use the member function
begin, which may cache the position. ThreadSanitizer visualizes this data race and complains about a previous write on line 24:
std::cout << i << " ";
On the contrary, iterating through a classical container such as
std::vector is thread-safe. There is an additional difference between classical container and views.
Propagation of Constness
Classical container model deep constness. They propagate their constness to their elements. This means that modifying elements of a constant container is impossible.
modifyConstRange(myVec) causes a compile-time error.
On the contrary, views model shallow constness. They do not propagate the constness to their elements. The elements can still be modified.
modifyConstRange(std::views::all(myVec)) is fine.
Coroutines are probably the most challenging part of C++20. My next post presents a guest post from Dian-Lun Lin. He will give a concise introduction to coroutines and exemplify his idea with a straightforward scheduler managing tasks.
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,