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

Learning how move semantics works

We know copies are expensive, especially heavy objects. The move semantics that were introduced in C++11 help us avoid expensive copies. The foundational concept behind std::move and std::forward is the rvalue reference. This recipe will show you how to use std::move.

How to do it...

Let's develop three programs to learn about std::move and its universal reference:

  1. Let's start by developing a simple program:
#include <iostream>
#include <vector>
int main ()
{
std::vector<int> a = {1, 2, 3, 4, 5};
auto b = std::move(a);
std::cout << "a: " << a.size() << std::endl;
std::cout << "b: " << b.size() << std::endl;
}
  1. Let's develop a second example:
#include <iostream>
#include <vector>
void print (std::string &&s)
{
std::cout << "print (std::string &&s)" << std::endl;
std::string str (std::move(s));
std::cout << "universal reference ==> str = " << str
<< std::endl;
std::cout << "universal reference ==> s = " << s << std::endl;
}
void print (std::string &s)
{
std::cout << "print (std::string &s)" << std::endl;
}
int main()
{
std::string str ("this is a string");
print (str);
std::cout << "==> str = " << str << std::endl;
return 0;
}
  1. Let's look at an example with the universal reference:
#include <iostream>
void print (std::string &&s)
{
std::cout << "print (std::string &&s)" << std::endl;
std::string str (std::move(s));
std::cout << "universal reference ==> str = " << str
<< std::endl;
std::cout << "universal reference ==> s = " << s << std::endl;
}
void print (std::string &s)
{
std::cout << "print (std::string &s)" << std::endl;
}
int main()
{
print ("this is a string");
return 0;
}

The next section will describe these three programs in detail.

How it works...

The output of the first program is as follows (g++ move_01.cpp and ./a.out):

In this program, auto b = std::move(a); does a couple of things:

  1. It casts the vector, a, to the rvalue reference.
  2. As it is an rvalue reference, the vector move constructor is called, which moves the content of the a vector to the b vector.
  3. a doesn't have the original data anymore, b has.

The output of the second program is as follows (g++ moveSemantics2.cpp and ./a.out):

In this second example, the str string we pass to the print method is an lvalue reference (that is, we can take the address of that variable), so it is passed by reference.

The output of the third program is as follows (g++ moveSemantics3.cpp and ./a.out):

In the third example, the method that's being called is the one with the universal reference as a parameter: print (std::string &&s). This is because we cannot take the address of this is a string, which means it is an rvalue reference.

It should be clear now that std::move doesn't actually move anything it is a function template that performs an unconditional cast to an rvalue, as we saw in the first example. This allows us to move (and not copy) the data to the destination and invalidate the source. The benefits of std::move are huge, especially every time we see an rvalue reference parameter to a method (T&&) that would probably* be a copy in the previous versions of the language (C++98 and before).

*Probably: it depends on compiler optimizations.

There's more...

std::forward is somewhat similar (but with a different purpose). It is a conditional cast to an rvalue reference. You are invited to learn more about std::forward, rvalue, and lvalue by reading the books referenced in the next section.

See also

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

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