- 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++ Iterator Design Pattern
- C++ Mediator Design Pattern
- C++ Memento Design Pattern
- C++ Observer Design Pattern
- C++ State Design Pattern
- C++ Strategy Design Pattern
- C++ Template Method Design Pattern
- C++ Visitor Design Pattern
- C++ Behavioural 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++ Chain of Responsibility
- C++ Structural Design Patterns
- C++ Adapter Pattern
- C++ Bridge Pattern
- C++ Composite Pattern
- C++ Decorator Pattern
- C++ Command Pattern
- C++ Proxy Pattern
- C++ Useful Resources
- C++ Questions and Answers
- C++ Quick Guide
- C++ Cheatsheet
- C++ STL Tutorial
- C++ Standard Library
- C++ Useful Resources
- C++ Discussion
- C++ Online Compiler
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;
}