Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
C++ System Programming Cookbook

You're reading from   C++ System Programming Cookbook Practical recipes for Linux system-level programming using the latest C++ features

Arrow left icon
Product type Paperback
Published in Feb 2020
Publisher Packt
ISBN-13 9781838646554
Length 292 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Onorato Vaticone Onorato Vaticone
Author Profile Icon Onorato Vaticone
Onorato Vaticone
Arrow right icon
View More author details
Toc

Table of Contents (13) Chapters Close

Preface 1. Getting Started with System Programming 2. Revisiting C++ FREE CHAPTER 3. Dealing with Processes and Threads 4. Deep Dive into Memory Management 5. Using Mutexes, Semaphores, and Condition Variables 6. Pipes, First-In First-Out (FIFO), Message Queues, and Shared Memory 7. Network Programming 8. Dealing with Console I/O and Files 9. Dealing with Time Interfaces 10. Managing Signals 11. Scheduling 12. Other Books You May Enjoy

Learning how Ranges work

The C++20 standard added Ranges, which are an abstraction of containers that allow the program to operate uniformly on containers' elements. Furthermore, Ranges represent a very modern and concise way of writing expressive code. We'll learn that this expressiveness is even greater with pipes and adaptors.

How to do it...

In this section, we'll write a program that will help us learn the main use case of Ranges in conjunction with pipes and adaptors. Given an array of temperatures, we want to filter out the negative ones and convert the positives (warm temperatures) into Fahrenheit:

  1. On a new source file, type the following code. As you can see, two lambda functions and a for range loop does the job:
#include <vector>
#include <iostream>
#include <ranges>

int
main()
{
auto temperatures{28, 25, -8, -3, 15, 21, -1};
auto minus = [](int i){ return i <= 0; };
auto toFahrenheit = [](int i) { return (i*(9/5)) + 32; };
for (int t : temperatures | std::views::filter(minus)
| std::views::transform(toFahrenheit))
std::cout << t << ' '; // 82.4 77 59 69.8
}

We'll analyze what's behind of Ranges in the next section. We'll also learn that Ranges are the first users of concepts.

How it works...

std::ranges represents a very modern way of describing a sequence of actions on a container in a readable format. This is one of the cases where the language improves readability.

Step 1 defines the temperatures vector, which contains some data. Then, we defined a lambda function that returns true if the input, i, is greater or equal to zero. The second lambda we defined converts i into Fahrenheit. Then, we looped over temperatures (viewable_range) and piped to the filter (called adaptor, in the scope of Ranges), which removed the negative temperatures based on the minus lambda function. The output is piped to another adaptor that converts every single item of the container so that the final loop can take place and print to the standard output.

C++20 provides another level on top of the one we used to iterate over the container's element, one that's more modern and idiomatic. By combining viewable_range with adaptors, the code is more concise, compact, and readable.

The C++20 standard library provides many more adaptors following the same logic, including std::views::all, std::views::take, and std::views::split.

There's more...

All of the adaptors are templates that use concepts to define the requirements that the specific adaptor needs. An example of this is as follows:

template<ranges::input_range V,                  std::indirect_unary_predicate<ranges::iterator_t<V>> Pred >
requires ranges::view<V> && std::is_object_v<Pred>
class filter_view : public ranges::view_interface<filter_view<V, Pred>>

This template is the std::views::filter we used in this recipe. This template takes two types: the first one is V, the input range (that is, the container), while the second one is Pred (which is the lambda function, in our case). We've specified two constraints for this template:

  • V must be a view
  • The predicate must be an object type: a function, lambda, and so on

See also

You have been reading a chapter from
C++ System Programming Cookbook
Published in: Feb 2020
Publisher: Packt
ISBN-13: 9781838646554
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Visually different images