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
  • Feedback & Rating feedback
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

Lambda expressions

A lambda expression (or lambda function) is a convenient way of defining an anonymous, small, and one-time use function to be used in the place right where it is needed. Lambda is particularly useful with Standard Template Library (STL), as we'll see.

How to do it...

In this section, we'll write some code in order to get familiar with lambda expressions. Although the mechanics are important, pay attention to the code readability with lambda, especially in conjunction with STL. Follow these steps:

  1. In this program, the lambda function gets an integer and prints it to standard output. Let's open a file named lambda_01.cpp and write the following code in it:
#include <iostream>
#include <vector>
#include <algorithm>
int main ()
{
std::vector<int> v {1, 2, 3, 4, 5, 6};
for_each (begin(v), end(v), [](int x) {std::cout << x
<< std::endl;});
return 0;
}
  1. In this second program, the lambda function captures a prefix by reference and prepends it to the integer in the standard output. Let's write the following code in a file called lambda_02.cpp:
#include <iostream>
#include <vector>
#include <algorithm>
int main ()
{
std::vector<int> v {1, 2, 3, 4, 5, 6};
std::string prefix ("0");
for_each (begin(v), end(v), [&prefix](int x) {std::cout
<< prefix << x << std::endl;});
return 0;
}
  1. Finally, we compile it with g++ lambda_02.cpp.

How it works...

In the first example, the lambda function just gets an integer as input and prints it. Note that the code is concise and readable. Lambda can capture the variables in scope by reference, &, or by value, =.

The output of the second program is as follows:

In the second example, the lambda captures the variable prefix by reference, making it visible to the lambda. Here, we captured the prefix variable by reference, but we might have captured any of the following:

  • All the variables by reference [&]
  • All the variables by value [=]
  • Specifying what variables to capture and how to capture them [&var1, =var2]

There are cases where we have to be explicit about the type to return, as in this case:

[](int x) -> std::vector<int>{
if (x%2)
return {1, 2};
else
return {3, 4};
});

The -> std::vector<int> operator, called trailing return type, tells the compiler that this lambda will return a vector of integers.

There's more...

Lambda can be decomposed into six parts:

  1. Capture clause: []
  2. Parameter list: ()
  3. Mutable specification: mutable
  4. Exception specification: noexcept
  5. Trailing return type: -> type
  6. Body: {}

Here, 1, 2, and 6 are mandatory.

Although optional, mutable specification and exception specification are worth having a look at as they might be handy in some circumstances. The mutable specification allows a by-value parameter to be modified by the body of the lambda. A variable in the parameter list is typically captured const-by-value, so the mutable specification just removes this restriction. The second case is the exception specification, which we can use to specify the exceptions the lambda might throw.

See also

The books Effective Modern C++ by Scott Meyers and The C++ Programming Language by Bjarne Stroustrup cover these topics in great detail.

Visually different images
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