- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Hello World
- C++ Omitting Namespace
- C++ Tokens
- C++ Constants/Literals
- C++ Keywords
- C++ Identifiers
- C++ Data Types
- C++ Numeric Data Types
- C++ Character Data Type
- C++ Boolean Data Type
- C++ Variable Types
- C++ Variable Scope
- C++ Multiple Variables
- C++ Input Output Operations
- C++ Basic Input/Output
- C++ Cin
- C++ Cout
- C++ Manipulators
- Type System & Data Representation
- C++ Modifier Types
- C++ Storage Classes
- C++ Constexpr Specifier
- C++ Numbers
- C++ Enumeration
- C++ Enum Class
- C++ References
- C++ Date & Time
- C++ Operators
- C++ Operators
- C++ Arithmetic Operators
- C++ Relational Operators
- C++ Logical Operators
- C++ Bitwise Operators
- C++ Assignment Operators
- C++ sizeof Operator
- C++ Conditional Operator
- C++ Comma Operator
- C++ Member Operators
- C++ Casting Operators
- C++ Pointer Operators
- C++ Operators Precedence
- C++ Unary Operators
- C++ Scope Resolution Operator
- C++ Control Statements
- C++ Decision Making
- C++ if Statement
- C++ if else Statement
- C++ Nested if Statements
- C++ switch Statement
- C++ Nested switch Statements
- C++ Loop Types
- C++ while Loop
- C++ for Loop
- C++ do while Loop
- C++ Foreach Loop
- C++ Nested Loops
- C++ Jump Statements
- C++ break Statement
- C++ continue Statement
- C++ goto Statement
- C++ Return Values
- C++ Strings
- C++ Strings
- C++ Loop Through a String
- C++ String Length
- C++ String Concatenation
- C++ String Comparison
- C++ Functions
- C++ Functions
- C++ Multiple Function Parameters
- C++ Recursive Function
- C++ Function Overloading
- C++ Function Overriding
- C++ Default Arguments
- C++ Arrays
- C++ Arrays
- C++ Multidimensional Arrays
- C++ Pointer to an Array
- C++ Passing Arrays to Functions
- C++ Return Array from Functions
- C++ Array Decay
- C++ Structure & Union
- C++ Structures
- C++ Unions
- C++ Class and Objects
- C++ Object Oriented
- C++ Classes & Objects
- C++ Class Member Functions
- C++ Class Access Modifiers
- C++ Static Class Members
- C++ Static Data Members
- C++ Static Member Function
- C++ Inline Functions
- C++ this Pointer
- C++ Friend Functions
- C++ Pointer to Classes
- C++ Constructors
- C++ Constructor & Destructor
- C++ Default Constructors
- C++ Parameterized Constructors
- C++ Copy Constructor
- C++ Constructor Overloading
- C++ Constructor with Default Arguments
- C++ Delegating Constructors
- C++ Constructor Initialization List
- C++ Dynamic Initialization Using Constructors
- C++ Destructors
- C++ Virtual Destructor
- C++ Inheritance
- C++ Inheritance
- C++ Multiple Inheritance
- C++ Multilevel Inheritance
- C++ Object-oriented
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
- C++ Virtual Function
- C++ Pure Virtual Functions & Abstract Classes
- C++ Override Specifiers
- C++ Final Specifiers
- C++ Design Patterns
- C++ Creational Design Patterns
- C++ Singleton Design Pattern
- C++ Factory Method Design Pattern
- C++ Abstract Factory Pattern
- C++ Prototype Design Pattern
- C++ Structural Design Patterns
- C++ Facade Design Pattern
- C++ File Handling
- C++ Files and Streams
- C++ Reading From File
- C++ Advanced
- C++ Exception Handling
- C++ Dynamic Memory
- C++ Move Semantics
- C++ Namespaces
- C++ Templates
- C++ Preprocessor
- C++ Signal Handling
- C++ Multithreading
- C++ Web Programming
- C++ Socket Programming
- C++ Concurrency
- C++ Advanced Concepts
- C++ Lambda Expression
- C++ nullptr
- C++ unordered_multiset
- C++ Structural Design Patterns
- C++ Adapter Pattern
- C++ Bridge Pattern
- C++ Composite Pattern
- C++ Decorator Pattern
- C++ Flyweight Pattern
Move Semantics in C++
Move semantics are used in C++ to transfer the ownership of resources from one object to another instead of copying them. It improves the performance as it avoid unnecessary copying of objects, reducing memory usage, improves efficiency, and efficiently handles the temporary objects like rvalue.
Why Move Semantics are Needed?
Before C++11, we used to copy the resource and it was less efficient and created duplicates. In C++11, move semantics were introduced to solve this problem of memory overhead and reducing the duplicates. Here are reasons why we need move semantics −
- Move semantics avoid copying of resources. It helps in avoiding duplicates and cause less memory overhead.
- It handles temporary objects like rvalues.
- It improves the performance by moving the resources instead of copying.
Expression Types in C++
An expression in C++ means any valid combination of variable, operator, constant, and function call that can give result. There are two types of expression which are as follows −
lvalue Expression
An lvalue is a type of expression in which object has a memory address and can be modified if it is not const. Example of lvalue can be a variable, array elements, and many more.
An lvalue reference is used to create a reference to an lvalue. It is denoted by (&) and is used for implementing copy semantics.
rvalue Expression
An rvalue is a type of expression that does not have a memory address and it represents a value that generally appears on the right side. It is a temporary expression that is about to get destroyed. Example of rvalue can be a constant, temporary object, and many more.
An rvalue reference is used to reference rvalues or temporary objects. It is denoted by (&&) and is used for implementing move semantics.
Here is an example of demonstrating lvalue and rvalue in C++. The x is an lvalue expression and x+5 is an rvalue expression.
#include <iostream>
using namespace std;
int main() {
// x is an lvalue
int x = 10;
// 'x + 5' is an rvalue
int y = x + 5;
cout << "Old x: " << x << endl;
cout << "Old y: " << y << endl;
// Demonstrating assignments
x = 20; // correct, as x is an lvalue
// (x + 5) = 15; // wrong, as x + 5 is an rvalue
cout << "New x: " << x << endl;
return 0;
}
The output of the above code is as follows −
Old x: 10 Old y: 15 New x: 20
The following example demonstrates lvalue reference(int &x) and rvalue reference(int &&x) −
#include <iostream>
using namespace std;
void printValue(int &x){
cout << "Calling with Lvalue reference: " << x << endl; // lvalue reference
}
void printValue(int &&x){
cout << "Calling with Rvalue reference: " << x << endl; // rvalue reference
}
int main(){
int a = 10;
printValue(a); // a is an lvalue
printValue(20); // 20 is an rvalue
return 0;
}
The output of the above code is as follows −
Calling with Lvalue reference: 10 Calling with Rvalue reference: 20
Why do Move Semantics Apply to Rvalues Only?
The move semantics is applied only to rvalues because, the rvalues are temporary objects, that are about to get destroyed. The rvalue will not affect the program in future as they are temporary and can be changed. So, applying move semantics on rvalue to transfer the ownership won't affect the program.
Here are the techniques that can be used to implement move semantics −
Move Constructor
A move constructor is a special constructor used to transfer the ownership of resources from a temporary object or rvalue to a new object using rvalue reference. The move constructor is automatically called when initializing an object with an rvalue.
#include <iostream>
#include <cstring>
using namespace std;
class MyString {
private:
char *data;
size_t length;
public:
// Regular constructor
MyString(const char *str){
length = strlen(str);
data = new char[length + 1];
strcpy(data, str);
cout << "Constructor called\n";
}
// Move constructor
MyString(MyString &&other) noexcept {
data = other.data; // transferring ownership
length = other.length;
other.data = nullptr;
other.length = 0;
cout << "Move constructor called\n";
}
// Destructor
~MyString(){
delete[] data;
cout << "Destructor called\n";
}
void print() const {
if (data)
cout << data << endl;
}
};
// Function returning a temporary object(rvalue)
MyString createString(){
return MyString("Temporary");
}
int main(){
// Calling move constructor
MyString s1 = createString();
s1.print();
return 0;
}
The output of the above code is as follows −
Constructor called Temporary Destructor called
In the above code, the desired output is not being displayed because of modern compiler apply RVO(Return Value Optimization).
Move Assignment Operator
The move assignment operator uses '(=)' and rvalue reference for move semantic. First, it releases the current resource of object and then take ownership of the source object's resources. Below is an example of move assignment operator −
#include <iostream>
#include <cstring>
using namespace std;
class MyString {
private:
char* data;
size_t length;
public:
// Regular constructor
MyString(const char* str) {
length = strlen(str);
data = new char[length + 1];
strcpy(data, str);
cout << "Constructor called\n";
}
// Move constructor
MyString(MyString&& other) noexcept {
data = other.data;
length = other.length;
other.data = nullptr;
other.length = 0;
cout << "Move constructor called\n";
}
// Move assignment operator
MyString& operator=(MyString&& other) noexcept {
if (this != &other) {
delete[] data;
data = other.data; // transferring ownership
length = other.length;
other.data = nullptr;
other.length = 0;
cout << "Move assignment operator called\n";
}
return *this;
}
// Destructor
~MyString() {
delete[] data;
cout << "Destructor called\n";
}
void print() const {
if (data)
cout << data << endl;
}
};
int main() {
MyString s1("Hello");
MyString s2("World");
cout << "Before move assignment:\n";
s1.print();
s2.print();
s1 = std::move(s2); // Move assignment operator called
cout << "After move assignment:\n";
s1.print();
s2.print(); // s2 is now empty
return 0;
}
The output of the above code is as follows −
Constructor called Constructor called Before move assignment: Hello World Move assignment operator called After move assignment: World Destructor called Destructor called
std::move() Function
Below is an example to transfer the ownership using the std::move() function −
#include <iostream>
#include <string>
#include <utility> // for move
using namespace std;
int main(){
cout << "Before move() function:";
string str1 = "Hello";
cout << "\nstring 1: " << str1 << endl;
cout << "\nAfter move() function:";
string str2 = move(str1);
cout << "\nstr2: " << str2;
cout << "\nstr1: " << str1 << "\n";
return 0;
}
The output of the above code is as follows −
Before move() function: string 1: Hello After move() function: str2: Hello str1:
Conclusion
In this chapter, we discussed about move semantics. Its main purpose is to transfer the ownership of resources of rvalues to save memory overhead and increase the efficiency of the code.