Thanks to C++23, constructing containers will become more convenient. Additionally, the ranges library got more new views.
C++23 is not such a significant standard as C++11 or C++20. It’s more in the tradition of C++17. This is mainly due to COVID-19 because the annual four face-to-face meetings went online. Essentially, the ranges library is the exception to this rule. The ranges will get a few crucial additions.
If you know more details about what you can expect about C++23 (before I write about it), study cppreference.com/compiler_support. Even better, read the excellent paper from Steve Downey (C++23 Status Report).
Constructing a container from a range was a complicated job. The following function range simulates python2’s range function. Python2’s range function is eager, and so is its range pendant: Additionally, Pythons range function returns a
list, but mine a
The calls in lines (1) – (4) should be pretty easy to read when looking at the output.
The first two arguments of the range call stand for the beginning and end of the created integers. The begin is included but not the end. The step size as the third parameter is, per default, 1. The step size should be negative when the interval [begin, end] decreases. If not, you get an empty list or an empty std::vector<int>.
I cheat a little in my range implementation. I use the function ranges::views::stride, which is not part of C++20. stride(n) returns the n-th element of the given range. I assume that
std::views::stride becomes part of C++23, but I’m not sure. Consequentially, I used the ranges v3 implementation in my example but not the C++20 implementation of the ranges library.
The if condition (begin < end) of the range function in line (1) should be quite easy to read. Create all numbers starting with begin (ranges::views::iota(begin)), take each n-th element (ranges::views::stride(stepsize), and do it as long as the boundary condition holds (ranges::views::take_while(boundary). Finally, push the integers on the std::vector<int>.
I use a little trick in the other case (line 2). I create the numbers [end++, begin++[, take them until the boundary condition is met, reverse them (ranges::views::reverse), and take each n-th element.
Now, let’s assume that std::views::stride is part of C++23. Thanks to std::ranges::to, it’s pretty easy to construct a Container. Here is the C++23-based implementation of the previous
Essentially, I replaced the
push_back operation on the
std::vector with the new call
std::ranges::to<std::vector>, and got rid of two lines of code. So far, no compiler supports this new convenient function to create a container. I created the new
range function based on my interpretation of the specification. If there is an error included, I will fix it.
Existing Algorithms in C++20
Before I show you the new views in C++23, here are the already existing ones in C++20:
New Views in C++23
Now, I want to present to you the new views. If possible, I will provide you with a short code example.
Creates a view that consists of tuples by applying a transformation function.
Here is an excellent example from cppreferene.com/zip_transform_view:
I added the output directly into the source code.
Creates a view that consists of tuples of references to adjacent elements. Additionally, You can apply a transformation function.
These examples are directly from the proposal P2321R2:
Creates a view by flattening the input range. Puts a delimiter between elements.
cppreference.com/join_with_view provides a nice example in which a space is the delimiter element.
Creates a view by dividing a range R into non-overlapping N-sized chunks. Additionally, you can apply a predicate.
Both code snippets use the prototype library fmt for the format library in C++20. fmt has a convenience function
fmt::print that may become part of C++23 as
Creates a view of N-tuples by taking a view and a number N.
The example is also from proposal P2443R1.
Last week, I made a poll and asked: “Which mentoring program should I implement next?” Honestly, this result surprised me a lot. I taught Design Patterns from 2004 to 2008 and assumed that you already knew them, and C++20 or Clean Code with C++ would win the poll. Consequentially, I changed my plan for my upcoming posts. My next big topic will be “Design Pattern and Architectural Pattern in C++”. When I finish this big topic, I will return to C++20 and C++23.
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, and Bhavith C Achar.
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|
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,