Today, I present a few utilities for calculating the midpoint of two values, checking if a
std::string starts or ends with a substring, and creating callables with
std::bind_front. These little utilities may not seem so minor when you need them.
Let’s start with arithmetical.
Midpoint and Linear Interpolation
std::midpoint(a, b)calculates the midpoint
(a + (b - a) / 2)of the integers, floating points, or pointers. If a and b are pointers, they must point to the same array object.
std::lerp(a, b, t)calculates the linear interpolation (a + t( b – a)). When t is outside the range [0, 1], it calculates the linear extrapolation.
The following program applies both functions.
The output of the program should be self-explanatory. If not, try it out on Compiler Explorer.
C++20 has convenience functions for creating arrays.
Creating Arrays and
std::make_shared, C++20 offers new ways to create a
std::shared_ptr from C-arrays.
std::to_array, creating a
std::array from a C-array is a straightforward job.
The lines (1), (2), (3), and (3) assert that the created std::array has the expected type and size.
Per design, a
std::array is as cheap and as fast as a C-array. If you want to know more about
std::array, and why you should not use a C-array, read my post “std::array – Dynamic Memory, no Thanks“.
std::array knows its size and supports the typical interface of each container of the Standard Template Library, such as
So far, all MSVC, Clang, GCC compilers support this convenient way to create a std::array. This observation does not hold for the next feature.
Create a std::shared_ptr of C-arrays
Since C++11, C++ has the factory function
std::make_shared to create a
std::shared_ptr. Since C++20,
std::make_shared also supports the creation of
std::shared_ptr of C-arrays.
s1 is a
std::shared_ptr of a C-array. All members are default initialized. s2 is a
std::shared_ptr of a C-array. Each element is initialized to
In contrast, the new two new member functions of
std::string are already available with a brand-new MSVC, Clang, or GCC compiler.
Check if a String starts with a Prefix or ends with a Suffix
std::string get a new member functions
ends_with which checks if a
std::string start or ends with a specified substring
Both member functions
ends_with are predicates. This means they return a boolean. You can invoke the member function
starts_with (line 1) with a
std::string (line 2), a
std::string_view (line 3), and a
char (line 4).
The following utility function in C++20 may wonder you.
std::bind_front (Func&& func, Args&& ... args) creates a callable wrapper for a callable
func. std::bind_front that can have an arbitrary number of arguments and binds its arguments to the front.
Now, to the part which may wonder you. Since C++11, we have
std::bind and lambda expression. To be pedantic
std::bind is available since Technical Report 1 (TR1). Both can be used as a replacement of
std::bind_front seems like the minor sister of
std::bind only supports the rearranging of arguments. Of course, there is a reason in the future to use
std::bind_front propagates exception specification of the underlying call operator.
The following program exemplifies that you can replace
std::bind, or lambda expressions.
Each call (lines 1 – 5) gets a callable taking two arguments and returns a callable taking only one argument because the first argument is bound to
2000. The callable is a function (1), a lambda expression (2), and a predefined function object (line 3).
_1 is a so-called placeholder (line 4) and stands for the missing argument. With lambda expression (line 5), you can directly apply one argument and provide an argument
b for the missing parameter. From the readability perspective,
std::bind_front is easier to read than
std::bind, or the lambda expression.
If you want to play with the example, use Compiler Explorer.
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,