Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Boost C++ Application Development  Cookbook

You're reading from   Boost C++ Application Development Cookbook Recipes to simplify your application development

Arrow left icon
Product type Paperback
Published in Aug 2017
Publisher Packt
ISBN-13 9781787282247
Length 438 pages
Edition 2nd Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Anton Polukhin Alekseevic Anton Polukhin Alekseevic
Author Profile Icon Anton Polukhin Alekseevic
Anton Polukhin Alekseevic
Arrow right icon
View More author details
Toc

Table of Contents (13) Chapters Close

Preface 1. Starting to Write Your Application FREE CHAPTER 2. Managing Resources 3. Converting and Casting 4. Compile-Time Tricks 5. Multithreading 6. Manipulating Tasks 7. Manipulating Strings 8. Metaprogramming 9. Containers 10. Gathering Platform and Compiler Information 11. Working with the System 12. Scratching the Tip of the Iceberg

Using the C++11 move emulation

One of the greatest features of the C++11 standard is rvalue references. This feature allows us to modify temporary objects, stealing resources from them. As you can guess, the C++03 standard has no rvalue references, but using the Boost.Move library, you can write a portable code that emulates them.

Getting ready

It is highly recommended that you are at least familiar with the basics of C++11 rvalue references.

How to do it...

  1. Imagine that you have a class with multiple fields, some of which are standard library containers:
namespace other { 
class characteristics{};
}

struct person_info {
std::string name_;
std::string second_name_;
other::characteristics characteristic_;
// ...
};
  1. It is time to add the move assignment and move constructors to it! Just remember that in the C++03 standard library, containers have neither move operators nor move constructors.
  2. The correct implementation of the move assignment is the same move constructing an object and swapping it with this. The correct implementation of the move constructor is close to the default construct and swap. So, let's start with the swap member function:
#include <boost/swap.hpp> 

void person_info::swap(person_info& rhs) {
name_.swap(rhs.name_);
second_name_.swap(rhs.second_name_);
boost::swap(characteristic_, rhs.characteristic_);
}
  1. Now, put the following macro in the private section:
    BOOST_COPYABLE_AND_MOVABLE(person_info) 
  1. Write a copy constructor.
  2. Write a copy assignment, taking the parameter as: BOOST_COPY_ASSIGN_REF(person_info).

 

  1. Write a move constructor and a move assignment, taking the parameter as BOOST_RV_REF(person_info):
struct person_info {
// Fields declared here
// ...
private:
BOOST_COPYABLE_AND_MOVABLE(person_info)
public:
// For the simplicity of example we will assume that
// person_info default constructor and swap are very
// fast/cheap to call.
person_info();

person_info(const person_info& p)
: name_(p.name_)
, second_name_(p.second_name_)
, characteristic_(p.characteristic_)
{}

person_info(BOOST_RV_REF(person_info) person) {
swap(person);
}

person_info& operator=(BOOST_COPY_ASSIGN_REF(person_info) person) {
person_info tmp(person);
swap(tmp);
return *this;
}

person_info& operator=(BOOST_RV_REF(person_info) person) {
person_info tmp(boost::move(person));
swap(tmp);
return *this;
}

void swap(person_info& rhs);
};
  1. Now, we have a portable fast implementation of the move assignment and move construction operators of the person_info class.

How it works...

Here is an example of how the move assignment can be used:

int main() {
person_info vasya;
vasya.name_ = "Vasya";
vasya.second_name_ = "Snow";

person_info new_vasya(boost::move(vasya));
assert(new_vasya.name_ == "Vasya");
assert(new_vasya.second_name_ == "Snow");
assert(vasya.name_.empty());
assert(vasya.second_name_.empty());

vasya = boost::move(new_vasya);
assert(vasya.name_ == "Vasya");
assert(vasya.second_name_ == "Snow");
assert(new_vasya.name_.empty());
assert(new_vasya.second_name_.empty());
}

The Boost.Move library is implemented in a very efficient way. When the C++11 compiler is used, all the macros for rvalues emulation are expanded to C++11-specific features otherwise (on C++03 compilers), rvalues are emulated.

There's more...

Have you noticed the boost::swap call? It is a really helpful utility function, which first searches for a swap function in the namespace of a variable (in our example, it's namespace other::), and if there is no matching swap function, it uses the std::swap.

See also

  • More information about emulation implementation can be found on the Boost website and in the sources of the Boost.Move library at http://boost.org/libs/move.
  • The Boost.Utility library is the one that contains boost::swap, and it has many useful functions and classes. Refer to http://boost.org/libs/utility for its documentation.
  • The Initializing a base class by the member of derived recipe in Chapter 2, Managing Resources.
  • The Making a noncopyable class recipe.
  • In the Making a noncopyable but movable class recipe, there is more info about Boost.Move and some examples on how we can use the movable objects in containers in a portable and efficient way.
You have been reading a chapter from
Boost C++ Application Development Cookbook - Second Edition
Published in: Aug 2017
Publisher: Packt
ISBN-13: 9781787282247
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
Banner background image