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

Using span

We may come across cases where we need to write a method but we'd like to have the flexibility to accept a plain array or STL containers as input. std::span solves this problem. It gives the user a view into a contiguous sequence of elements. This recipe will teach you how to use it.

How to do it...

In this recipe, we'll write a method with one parameter (std::span) that can be used in different contexts. Then, we'll highlight the flexibility it offers:

  1. Let's start by adding the includes we need. Then, we need to define the print method by passing the container variable of the std::span type:
#include <iostream>
#include <vector>
#include <array>
#include <span>

void print(std::span<int> container)
{
for(const auto &c : container)
std::cout << c << "-";
}
  1. In main, we want to print our arrays by calling the print method:
int main()
{
int elems[]{4, 2, 43, 12};
print(elems);

std::vector vElems{4, 2, 43, 12};
print(vElems);
}

Let's see how this works.

How it works...

std::span describes an object that refers to a contiguous sequence of elements. The C++ standard defines an array as having a contiguous portion of memory. This definitely simplifies the std::span implementation, since a typical one includes a pointer to the first element of the sequence and the size.

Step 1 defines the print method of passing the std::span, which we can read as a sequence of integers. Any array type that has contiguous memory will be seen from the method as a sequence.

Step 2 uses the print method with two different arrays, one C-style and the second an std::vector part of the STL library. Since both arrays are defined in a contiguous portion of memory, std::span is able to seamlessly manage them.

There's more...

Our method considers std::span with the int type. You might need to make the method generic. In this case, you'd need to write something like this:

template <typename T>
void
print(std::span<T> container)
{
for(const auto &c : container)
std::cout << c << "-";
}

As we learned in the Understanding concepts recipe, it is wise to specify some requirements in this template. Therefore, we might write to the following:

template <typename T>
requires Integral<T>
void
print(std::span<T> container)
{
for(const auto &c : container)
std::cout << c << "-";
}

The requires Integral<T> would make explicit the needs of an Integral type for the template.

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 €18.99/month. Cancel anytime