Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
roth_stephan_clean_c20_sustainable_software_development_patt-1.pdf
Скачиваний:
25
Добавлен:
27.03.2023
Размер:
7.26 Mб
Скачать

Chapter 5 Advanced Concepts of Modern C++

Take Advantage of Boost

I can’t give a broad introduction into the famous Boost library (www.boost.org, distributed under the Boost Software License, Version 1.0) here. The library (in fact, it is a library of libraries) is too big and too powerful, and discussing it in detail is beyond the scope of this book. Furthermore, there are numerous good books and tutorials about Boost.

But I think that it is very important to know about this library and its content. Many problems and challenges that C++ developers face in their daily work can be pretty well solved with libraries from Boost.

Beyond that, Boost is a kind of “incubator” for several libraries that are sometimes accepted to become part of the C++ language standard, if they have a certain level of maturity. Be careful, that does not necessarily mean that they are fully compatible! For instance, std::thread (part of the standard since C++11) is partially equal to Boost. Thread, but there are some differences. For example, the Boost implementation supports thread cancellation, something that is available in the Standard Library only since C++20 (std::jthread). On the other hand, C++11 supports std::async, but Boost does not.

From my perspective, it is worth it to know the libraries from Boost, and to remember when you have a suitable problem that can be properly solved by them.

More Libraries That You Should Know About

Apart from Standard Library containers, <algorithm>, Ranges, and Boost, there are some more libraries out there that you might take into consideration when writing your code. Here is an incomplete list of libraries that are worth looking at when you are confronted with a certain suitable problem:

•\ Atomic types (<atomic>): A collection of templates and types available since C++11 that different threads can simultaneously operate on without raising undefined behavior (data races; see the sidebar about “Atomic Operations” in the section about smart

pointers). The central element is the class template std::atomic<T>, which can be used to define atomic types. For all integral data types, corresponding aliases are predefined, for example atomic_int32_t for std::atomic<int32_t>.

194

Chapter 5 Advanced Concepts of Modern C++

•\ Date and time utilities (<chrono>): Since C++11, the language provides a collection of types to represent clocks, time points, and durations. And with the latest standard C++20, dates and time zones have also been added. For instance, you can represent time intervals with the help of std::chrono::duration. And with std::chrono::system_clock, a system-wide real-time clock is

available. You can use the library since C++11 by just including the

<chrono> header.

•\ Pseudo-random number generator library (<random>): A library available since C++11 that provides classes for generating random and pseudo-random numbers. This library is a much better, more modern, and more powerful alternative to the old C library function combination srand() with rand(). With the <random> header, developers get a selection of Random Number Generators (RNG) with different engines (Minimum standard, 32-bit and 64-­ bit Mersenne Twister, etc.) and distributions (Normal, Uniform, Bernoulli, etc.) at hand.

•\ Regular expressions library (<regex>): Since C++11, a regular expressions library is available that can be used to perform pattern matching within strings. Also the replacement of text within a string based on regular expressions is supported. You can use the library since C++11 by just including the <regex> header.

•\ Filesystem library (<filesystem>): Since C++17, the Filesystem library has become part of the standard. Before it became part of the mainline C++ standard, it was a technical specification (ISO/ IEC TS 18822:2015). The operational system independent library provides various facilities for performing operations on file systems

and their components. With the help of <filesystem> you can create directories, copy files, iterate over directory entries, retrieve the size of a file, etc. You can use the library since C++17 by just including the

<filesystem> header.

Tip  If you are currently not working according to the C++17 standard or higher, Boost.Filesystem could be an alternative.

195