Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying C++ System Programming Cookbook
  • Table Of Contents Toc
C++ System Programming Cookbook

C++ System Programming Cookbook

By : Onorato Vaticone
3.8 (4)
close
close
C++ System Programming Cookbook

C++ System Programming Cookbook

3.8 (4)
By: Onorato Vaticone

Overview of this book

C++ is the preferred language for system programming due to its efficient low-level computation, data abstraction, and object-oriented features. System programming is about designing and writing computer programs that interact closely with the underlying operating system and allow computer hardware to interface with the programmer and the user. The C++ System Programming Cookbook will serve as a reference for developers who want to have ready-to-use solutions for the essential aspects of system programming using the latest C++ standards wherever possible. This C++ book starts out by giving you an overview of system programming and refreshing your C++ knowledge. Moving ahead, you will learn how to deal with threads and processes, before going on to discover recipes for how to manage memory. The concluding chapters will then help you understand how processes communicate and how to interact with the console (console I/O). Finally, you will learn how to deal with time interfaces, signals, and CPU scheduling. By the end of the book, you will become adept at developing robust systems applications using C++.
Table of Contents (13 chapters)
close
close

Understanding concepts

A concept is a compile-time predicate that's used in conjunction with templates. The C++20 standard definitely boosted generic programming by providing more compile-time opportunity for the developer to communicate its intention. We can visualize concepts such as requirements (or constraints) the user of the template must adhere to. Why do we need concepts? Do you have do define concepts by yourself? This recipe will answer these and many more questions.

How to do it...

In this section, we will develop a concrete template example using concepts:

  1. We want to create our own version of the std::sort template function from the C++ standard library. Let's start by writing the following code in a .cpp file:
#include <algorithm>
#include <concepts>

namespace sp
{
template<typename T>
requires Sortable<T>
void sort(T& container)
{
std::sort (begin(container), end(container));
};
}
  1. Now, let's use our new template class with the constraint that the type we pass, an std::vector, must be sortable; otherwise, the compiler will notify us:
int main()
{
std::vector<int> myVec {2,1,4,3};
sp::sort(vec);

return 0;
}

We'll look at the details in the next section.

How it works...

I strongly believe concepts were the missing feature. Before them, a template didn't have a well-defined set of requirements, nor, in the case of a compilation error, a simple and brief description of it. These are the two pillars that drove the design of the concepts feature.

Step 1 includes the algorithms include for the std::sort method and the concepts header. To not confuse the compiler and ourselves, we encapsulated our new template in a namespace, sp. As you can see, there is a very minimal difference compared to the classical templates we used to use and the difference is with the requires keyword.

requires communicates to the compiler (and to the template user) that this template is only valid with a T Sortable type (Sortable<T>). OK; what is Sortable? This is a predicate that is only satisfied if it is evaluated to true. There are other ways to specify a constraint, as follows:

  • With the trailing requires:
template<typename T>
void sort(T& container) requires Sortable<T>;
  • As a template parameter:
template<Sortable T>
void sort(T& container)

I personally prefer the style in the How to do it... section as it is more idiomatic and, more importantly, allows us to keep all the requires together, like so:

template<typename T>
requires Sortable<T> && Integral<T>
void sort(T& container)
{
std::sort (begin(container), end(container));
};

In this example, we want to communicate that our sp::sort method is valid with type T, which is Sortable and Integral, for whatever reason.

Step 2 simply uses our new customized version of sort. To do this, we instantiated a vector (which is Sortable!) and passed in input to the sp::sort method.

There's more...

There might be cases where you need to create your own concept. The standard library contains plenty of them, so it is a remote probability that you'd need one. As we learned in the previous section, a concept is a predicate if and only if it is evaluated as true. The definition of a concept as a composite of two existing ones might look like this:

template <typename T>
concept bool SignedSwappable() 
{ return SignedIntegral<T>() && Swappable<T>(); }

Here, we can use the sort method:

template<typename T>
requires SignedSwappable<T>
void sort(T& container)
{
std::sort (begin(container), end(container));
};

Why is this cool? For a couple of reasons:

  • It lets us immediately know what the template expects without getting lost in implementation details (that is, the requirements or constraints are explicit).
  • At compile time, the compiler will evaluate whether the constraints have been met.

See also

  • A Tour of C++, Second Edition, B. Stroustrup: Chapter 7.2 and Chapter 12.7 for a complete list of concepts defined in the standard library.
  • https://gcc.gnu.org/projects/cxx-status.html for a list of C++20 features mapped with GCC versions and status.
CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
C++ System Programming Cookbook
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist download Download options font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon