Vlad's blog

In programming veritas

Posts Tagged ‘C++0x

C++0x: rvalue expressions and move constructor

leave a comment »

I continue to write about the language features that were added to the new standard C++ 0x. This time we discuss how we can reduce the number of copy operations using move semantics for copy constructor, which is used for rvalue expressions.
But before we talk about the move constructor let’s discuss lvalue and rvalue expressions.
Every C++ expression is either an lvalue or an rvalue. lvalue expression is an object that has a name. All variables are lvalue. rvalue is a temporary object that does not exist outside of the expression that created it. In the following example a is an lvalue, the result of the expression 2 + 2 – rvalue.

int a = 2+2;

Technically evaluating the expression 2+2 creates a temporary object that is then copied into a variable a . For built-in types, this will not be much cost in terms of performance. For user-defined types, which are demanding the copy operation, the costs associated with calling the copy constructor can be substantial.
In the following example string str1 is assigned the result of concatenating the string str2 and str3.

std::string str1;
std::string str2("Hello");
std::string str3(",World!");
str1 = str2 + str3;

As a result of the addition a temporary object is created that contains the string “Hello, World!”. Then assignment operator is called for str1 that copies the contents of the temporary object in str1. After that, the destructor is called for deleting of the temporary object.
What happens if instead of copying the temporary object we use move semantics? The temporary object is still deleted and will not be used anywhere, so its data (a pointer to a string, for example) you can simply assign the appropriate field of the object str1 without calling an expensive copy operation.But this method is only valid for rvalue expressions. Hence, we need some way to distinguish when the assignment operator is called with the lvalue expression, and when to rvalue.
Read the rest of this entry »

Written by vsukhachev

December 9, 2011 at 2:47 am

Posted in Development

Tagged with

Smart pointers in C++0x

leave a comment »

Probably, it is difficult to find a programmer for C++, who is never used smart pointers. Dozens of implementations appeared that differ in strategy of ownership, implicit conversion and multithreading support. Before the advent of C++0x standard library offered only std::auto_ptr, which is not suitable for many situations. Of course, you could use Boost or Loki but only in C++0x smart pointers officially included in the standard.
So, what is smart pointer? This is a class that mimics the semantics of a pointer with the implementation of the -> operator and the operator * and frees the programmer from the control of the resource to which it refers. As a rule, smart pointers are implemented as template classes, providing access to the methods of the object referenced by. But before we begin a detailed review of new opportunities for C++0x in terms of smart pointers make a small digression.

Copy and move semantics

The concept of copy and move semantics is closely related to the topic of smart pointers, and is itself a topic for separate discussion. Here we discuss the basic definitions. Based on its definition copy semantics implies calling the copy constructor or assignment operator. Consider the following example.

void swap(int& a, int& b)
{
    int tmp = a;
    a = b;
    b = tmp;
}

There are three copy operations in this example. This is acceptable for int, but let us assume that we have to swap two strings. Let’s say string class has member char* data that points to null delimited string and member size_t size which is a length of string. Below is the implementation that uses move semantics.

void swap(string& a, string& b)
{
    const char* p = a.data();
    size_t sz = a.size();
    a.data() = b.data();
    a.size = b.size();
    b.data = p;
    b.size = sz;
}

Notice we were able to avoid using of time consuming copy operations. It is recommended to implement publicly available swap method for you classes. It can dramatically improve the performance when you use STL containers.

class A
{
    public:
        void swap(A& rhs); 
};

Read the rest of this entry »

Written by vsukhachev

November 4, 2011 at 11:25 am

Posted in Development

Tagged with

Lamda expressions in C++0x

leave a comment »

New C++0x standard introduces a lot of handy features and lamda expression is one of the most powerfull constructions out of the box. It can significantly reduce a complexity of many routine tasks. This post is brief introduction of lambda syntax and techniques that can be used in conjunction with lambda expressions.
For the sake of simplicity suppose we want to print out std::vector. STL recommends using of std::for_each algorithm that receives a functor as the final argument.

struct Print
{
	void operator()(int n) const
	{
		std::cout << n << std::endl;
	};
};

std::vector<int> a;
a.push_back(1);
a.push_back(2);
a.push_back(3);

std::for_each(a.begin(), a.end(), Print());

Read the rest of this entry »

Written by vsukhachev

August 18, 2011 at 10:15 am

Posted in Development

Tagged with ,