Articles & Books

Six Handy Operations for String Processing in C++20/23 -- Bartlomiej Filipek

sixhandy-barto.pngIn this article, we’ll explore six practical string processing operations introduced in C++20 and C++23. These features represent an evolution in this crucial area, covering a spectrum of operations from searching and appending to creation and stream handling.

Six Handy Operations for String Processing in C++20/23

by Bartlomiej Filipek

From the article:

Let’s start with a simple yet long-awaited feature…

1. contains(), C++23

Finally, after decades of standardization, we have a super easy way to check if there’s one string inside the other. No need to use .find(str) != npos!

Before C++23:

sixhandy-1.png

And now, thanks to the proposal: P1679R3:
sixhandy-2.png
As you can see, the new approach with contains() is more straightforward and intuitive.

How to Print Unicode Text to the Windows Console in C++ -- Giovanni Dicanio

Have you ever wondered how to correctly print Unicode text to the Windows console in your C++ programs? Maybe you tried something, and you got meaningless output, or even an assertion failure at runtime, pointing to some obscure code in the CRT? Well, then this article is for you!

How to Print Unicode Text to the Windows Console in C++

by Giovanni Dicanio

From the blog post:

Suppose that you want to print out some Unicode text to the Windows console. From a simple C++ console application created in Visual Studio, you may try this line of code inside main:

std::wcout << L"Japan written in Japanese: x65e5x672c (Nihon)n";

As you can see, the Japanese kanjis are not printed. Moreover, even the “standard ASCII” characters following those (i.e.: “(Nihon)”) are missing. There’s clearly a bug in the above code.

How can you fix that?

Well, the missing piece is ...

Breaking Down C++20 Callable Concepts -- John Farrier

Learn about C++20's Callable Concepts, how they improve on SFINAE, and how to use them in your own code.

Breaking Down C++20 Callable Concepts

By John Farrier

From the article:

C++20 Callable Concepts refer to a powerful feature introduced in the C++20 standard that allows developers to specify and enforce constraints on function objects, lambdas, and other callable entities in a more expressive and type-safe manner. Callable Concepts enable the compiler to check if a given callable meets specific requirements, such as having certain member functions or satisfying particular type traits, before allowing it to be used in a template or function. This helps improve code readability, maintainability, and error detection, as it provides clearer and more structured ways to define the expected behavior and capabilities of callables in C++ code.

 

Easily Protect Your C++ Code Against Integer Overflow With SafeInt -- by Giovanni Dicanio

Integer overflows can be hidden inside your C++ code and can cause subtle nasty bugs.

Fortunately, it's easy to guard your code against those subtle bugs, thanks to an open-source easy-to-use library:

Protecting Your C++ Code Against Integer Overflow Made Easy by SafeInt

by Giovanni Dicanio

From the article:

In previous blog posts I discussed the problem of integer overflow and some subtle bugs that can be caused by that. (...)

Of course, writing this kind of complicated check code each time there is a sum operation that could potentially overflow would be excruciatingly cumbersome, and bug prone! (...)

Fortunately, you don’t have to do all that work! In fact, there is an open source library that does exactly that! This library is called SafeInt. (...)

The code will still look nice and simple, but safety checks will happen automatically under the hood. Thank you very much SafeInt and C++ operator overloading!

 

When an Empty Destructor is Required -- Andreas Fertig

me.pngIn my previous post, "Why you shouldn't provide an empty destructor," we discussed the importance of not providing an empty destructor in most cases. However, in this post, we'll explore a rare exception to this rule that arises when using the PImpl idiom to hide implementation details, specifically in situations where the unique_ptr encounters issues with incomplete types. Let's delve into this exception and learn when it's necessary to provide an empty destructor.

When an Empty Destructor is Required

by Andreas Fertig

From the article:

As you probably know from life, not only programming. There is no rule without an exception. Let's talk about the one case that requires an empty destructor (yes, I know it sounds silly). Suppose you have the following code:

In file included from <source>:2:
In file included from /opt/compiler-explorer/gcc-12.2.0/lib/gcc/x86_64-linux-gnu/12.2.0/../../../../include/c++/12.2.0/memory:76:
/opt/compiler-explorer/gcc-12.2.0/lib/gcc/x86_64-linux-gnu/12.2.0/../../../../include/c++/12.2.0/bits/unique_ptr.h:93:16: error: invalid application of 'sizeof' to an incomplete type 'Orange'
        static_assert(sizeof(_Tp)>0,
                      ^~~~~~~~~~~
/opt/compiler-explorer/gcc-12.2.0/lib/gcc/x86_64-linux-gnu/12.2.0/../../../../include/c++/12.2.0/bits/unique_ptr.h:396:4: note: in instantiation of member function 'std::default_delete<Orange>::operator()' requested here
          get_deleter()(std::move(__ptr));
          ^
<source>:4:7: note: in instantiation of member function 'std::unique_ptr<Orange>::~unique_ptr' requested here
class Apple
      ^
<source>:6:27: note: forward declaration of 'Orange'
    std::unique_ptr<class Orange> mOrange{};
                          ^
1 error generated.
Compiler returned: 1
 
Essentially, this message tells you that the std::unique_ptr cannot instantiate the required destructor for itself typed with Orange. The simple reason is that, at this point, Orange has no visible destructor. Which is the entire point of the PImpl idiom. What now?

Boost 1.84.0 has been released

space.pngspace.pnggetboost.pngRelease 1.84.0 of the Boost C++ Libraries is now available.

These open-source libraries work well with the C++ Standard Library, and are usable across a broad spectrum of applications. The Boost license encourages both commercial and non-commercial use.

Release 1.84.0 contains two new libraries and numerous enhancements and bug fixes for existing libraries.

New Libraries

  • Cobalt: Basic algorithms and types for C++20 coroutines, from Klemens Morgenstern.
  • Redis: Redis async client library built on top of Boost.Asio, from Marcelo Zimbres Silva.

Here are some useful links for 1.84.0.


Marshall Clow and Glen Fernandes managed this release - we very much appreciate their continued hard work!

C++20, Spans, Threads and Fun -- Bartlomiej Filipek

C++20_Spans_Threads_and_Fun.pngIn this post, we’ll have fun using C++20’s spans to process data on multiple threads. What’s more, we’ll be equipped with the latest concurrency features from C++20.

C++20, Spans, Threads and Fun

by Bartlomiej Filipek

From the article:

Our task is relatively simple but can be extended to various multi-phase computations.

We need to initialize a container with numbers, filter them, then build a histogram.

Here’s the overview of the process: