Updated April 20, 2023
Introduction to C++ Move Semantics
The contents of the objects can be moved between the objects instead of copying the contents from one object to another object by making use of Move Semantics in C++ and the move is possible when we are trying to pass an object to the function or an object is being returned from the function. If and only if the object to be passed to a function or that is to be returned from the function is an rvalue or if the special member move functions is defined by the class object meaning whenever the move happens, the data in the old object is removed and the same data is updated in the new object.
Syntax of Move Semantics in C++:
std::move(obj)
- Where obj is the content of the object which is to be moved to another object.
- The return value is an rvalue reference to the object.
Working of Move Semantics in C++
- Whenever there is a need to move the contents of the objects between the objects instead of copying the contents from one object to another object, we make use of Move Semantics in C++.
- We were making use of return by pointer and pass out by reference in the earlier versions of C++ to move the contents of the objects from one object to another object.
- With the release of C++ version 11, the concept of move semantics is introduced to move the contents of the objects from one object to another.
- The value whose address can be referenced in called lvalue and the value that exists only during the expression evaluation is called rvalue.
- The operator & can be used on the lvalues and the operator && can be used on the rvalues.
- The move is possible when we are trying to pass an object to the function or an object is being returned from the function, if and only if the object to be passed to a function is an rvalue or if the special member move functions is defined by the class object meaning whenever the move happens, the data in the old object is removed and the same data is updated in the new object.
Examples of C++ Move Semantics
Given below are the examples mentioned :
Example #1
C++ program to demonstrate Move Semantics to swap the contents of the given two objects.
Code:
#include <iostream>
using namespace std;
//a class called check is defined
class check
{
public:
//a constructor is defined to initialize the length and value of the given object
explicit check(size_t length)
: varLength(length), valData(new int[length]) {}
//a move constructor is defined to initialize the move operation
check(check&& other)
{
valData = other.valData;
varLength = other.varLength;
other.valData = nullptr;
other.varLength = 0;
}
// the assignment of move operation is performed
check& operator=(check&& other) noexcept
{
valData = other.valData;
varLength = other.varLength;
other.valData = nullptr;
other.varLength = 0;
return *this;
}
//getvarLength function returns the length of the object
size_t getvarLength()
{
return varLength;
}
//swap function to swap the content of the objects by making use of move semantics
void swap(check& other)
{
check temp = move(other);
other = move(*this);
*this = move(temp);
}
//getvarLength function returns the value of the object
int* getvalData()
{
return valData;
}
private:
int *valData;
size_t varLength;
};
//main method is called within which the swap on the given two objects is called
int main()
{
check firstobj(100), secondobj(200);
cout << "The length of the first object is:" << firstobj.getvarLength() << endl;
cout << "The length of the second object is:" << secondobj.getvarLength() << endl;
cout << "The value stored in the first object is:" << firstobj.getvalData() << endl;
cout << "The value stored in the second object is:" << secondobj.getvalData() << endl;
swap(firstobj,secondobj);
cout << "The length of the first object is:" << firstobj.getvarLength() << endl;
cout << "The length of the second object is:" << secondobj.getvarLength() << endl;
cout << "The value stored in the first object is:" << firstobj.getvalData() << endl;
cout << "The value stored in the second object is:" << secondobj.getvalData() << endl;
return 0;
}
Output:
Explanation:
- In the above program, a class called check is defined. Then a constructor is defined to initialize the length and value of the given object. Then a move constructor is defined to initialize the move operation. Then the assignment of move operation is performed. Then getvarLength function is written which returns the length of the object.
- Then swap function to swap the content of the objects by making use of move semantics. Then getvarLength function returns the value of the object. Then the main method is called within which the swap on the given two objects is called.
Example #2
C++ program to demonstrate Move Semantics to swap the contents of the given two objects.
Code:
#include <iostream>
using namespace std;
//a class called check is defined
class check
{
public:
//a constructor is defined to initialize the length and value of the given object
explicit check(size_t length)
: varLength(length), valData(new int[length]) {}
//a move constructor is defined to initialize the move operation
check(check&& other)
{
valData = other.valData;
varLength = other.varLength;
other.valData = nullptr;
other.varLength = 0;
}
// the assignment of move operation is performed
check& operator=(check&& other) noexcept
{
valData = other.valData;
varLength = other.varLength;
other.valData = nullptr;
other.varLength = 0;
return *this;
}
//getvarLength function returns the length of the object
size_t getvarLength()
{
return varLength;
}
//swap function to swap the content of the objects by making use of move semantics
void swap(check& other)
{
check temp = move(other);
other = move(*this);
*this = move(temp);
}
//getvarLength function returns the value of the object
int* getvalData()
{
return valData;
}
private:
int *valData;
size_t varLength;
};
//main method is called within which the swap on the given two objects is called
int main()
{
check firstobj(10), secondobj(20);
cout << "The length of the first object is:" << firstobj.getvarLength() << endl;
cout << "The length of the second object is:" << secondobj.getvarLength() << endl;
cout << "The value stored in the first object is:" << firstobj.getvalData() << endl;
cout << "The value stored in the second object is:" << secondobj.getvalData() << endl;
swap(firstobj,secondobj);
cout << "The length of the first object is:" << firstobj.getvarLength() << endl;
cout << "The length of the second object is:" << secondobj.getvarLength() << endl;
cout << "The value stored in the first object is:" << firstobj.getvalData() << endl;
cout << "The value stored in the second object is:" << secondobj.getvalData() << endl;
return 0;
}
Output:
Explanation:
- In the above program, a class called check is defined. Then a constructor is defined to initialize the length and value of the given object. Then a move constructor is defined to initialize the move operation. Then the assignment of move operation is performed. Then getvarLength function is written which returns the length of the object.
- Then swap function to swap the content of the objects by making use of move semantics. Then getvarLength function returns the value of the object. Then the main method is called within which the swap on the given two objects is called.
Recommended Articles
This is a guide to C++ Move Semantics. Here we discuss the introduction to C++ Move Semantics with how does it work and programming examples. You may also have a look at the following articles to learn more –