Cambo Freelance
HomeServicesArticlesTutorialsTeamCoursesContact
Cambo Freelance

Professional freelance team from Cambodia delivering technology-driven solutions with cultural insight and modern expertise.

Send us a message

Ready to start your project? Get in touch with our team.

Contact Us

Services

  • Web & Mobile Development Services
  • Graphic Design & Branding Services

Useful Links

  • Home
  • Services
  • Learning
  • About Us
  • Contact
  • Pricing

© 2026 Cambo Freelance. រក្សាសិទ្ធិទាំងអស់។

TelegramFacebookLinkedInEmail
HomeTutorialsC++C++ Move Semantics
⚡C++ Tutorials

50 lessons

Beginner(21)1C++ Introduction2C++ Getting Started3C++ Syntax4C++ Output5C++ Comments6C++ Variables7C++ Data Types8C++ Constants9C++ User Input10C++ Operators11C++ Strings12C++ Math13C++ Booleans14C++ Conditions15C++ Switch16C++ Loops17C++ Break and Continue18C++ Arrays19C++ Functions20C++ References21C++ Pointers
Intermediate(19)22C++ Classes23C++ Objects24C++ Constructors25C++ Access Specifiers
Advanced(10)41C++ Smart Pointers42C++ Templates43C++ Lambda Functions44
⚡

C++ Tutorials

Lesson 44 of 50

All lessons
Beginner (21)1C++ Introduction2C++ Getting Started
26
C++ Encapsulation
27C++ Inheritance
28C++ Polymorphism
29C++ Abstraction
30C++ Files
31C++ Exception Handling
32C++ Namespaces
33C++ Structures
34C++ Enumerations
35C++ STL Introduction
36C++ Vectors
37C++ Maps
38C++ Sets
39C++ Queues
40C++ Stacks
C++ Move Semantics
45C++ Multithreading
46C++ Design Patterns
47C++ Memory Management
48C++17 Features
49C++20 Features
50C++ Performance Optimization
3
C++ Syntax
4C++ Output
5C++ Comments
6C++ Variables
7C++ Data Types
8C++ Constants
9C++ User Input
10C++ Operators
11C++ Strings
12C++ Math
13C++ Booleans
14C++ Conditions
15C++ Switch
16C++ Loops
17C++ Break and Continue
18C++ Arrays
19C++ Functions
20C++ References
21C++ Pointers
Intermediate (19)22C++ Classes23C++ Objects24C++ Constructors25C++ Access Specifiers26C++ Encapsulation27C++ Inheritance28C++ Polymorphism29C++ Abstraction30C++ Files31C++ Exception Handling32C++ Namespaces33C++ Structures34C++ Enumerations35C++ STL Introduction36C++ Vectors37C++ Maps38C++ Sets39C++ Queues40C++ Stacks
Advanced (10)41C++ Smart Pointers42C++ Templates43C++ Lambda Functions44C++ Move Semantics45C++ Multithreading46C++ Design Patterns47C++ Memory Management48C++17 Features49C++20 Features50C++ Performance Optimization
⚡C++AdvancedLesson 44 of 50CamboFreelanceJune 19, 2026

C++ Move Semantics

Understand C++ move semantics: rvalue references, std::move, move constructors, and move assignment for high-performance code.


Tutorials › C++ › Move Semantics
Advanced11 min readLesson 44 of 50

lvalues and rvalues

An lvalue has a name and a persistent memory address. An rvalue is a temporary value with no address.

int x = 42;      // x is lvalue; 42 is rvalue
string s = "hi"; // s is lvalue; "hi" is rvalue
string t = s;    // copy: expensive for large strings

std::move and Move Constructor

std::move transfers ownership of resources instead of copying them — no memory allocation, much faster for large objects:

#include <iostream>
#include <vector>
#include <string>
using namespace std;

class Buffer {
  public:
    vector<int> data;
    Buffer(int size) : data(size, 0) {
        cout << "Constructed, size=" << data.size() << endl;
    }
    // Move constructor
    Buffer(Buffer&& other) noexcept : data(move(other.data)) {
        cout << "Moved!" << endl;
    }
};

int main() {
    Buffer b1(1000000);
    Buffer b2(move(b1));   // move, not copy
    cout << "b1 size: " << b1.data.size() << endl;   // 0 (stolen)
    cout << "b2 size: " << b2.data.size() << endl;   // 1000000
    return 0;
}

Output:

Constructed, size=1000000
Moved!
b1 size: 0
b2 size: 1000000

Rvalue References

void process(string&& s) {   // rvalue reference parameter
    cout << "Moving: " << s << endl;
}

process("temporary");       // OK — rvalue
string name = "Alice";
process(move(name));        // cast lvalue to rvalue
Rule of Five: If you define a destructor, copy constructor, or copy assignment, also define move constructor and move assignment operator.

Exercise

Create a vector of 5 strings. Use std::move to move (not copy) one string into a new variable. Verify the original is empty after the move.

Show Solution
#include <iostream>
#include <vector>
#include <string>
using namespace std;

int main() {
    vector<string> words = {"alpha","beta","gamma","delta","epsilon"};
    string moved = move(words[2]);
    cout << "Moved: "    << moved    << endl;   // gamma
    cout << "Original: " << words[2] << endl;   // (empty)
    return 0;
}

Quiz

  1. What does std::move(x) actually do?

    • A) Copies x to a new location
    • B) Casts x to an rvalue reference, enabling the move constructor
    • C> Deletes x
    • D) Swaps x with another variable
    Answer

    B) std::move is just a cast — it enables the move constructor/assignment to steal resources.

Summary

  • Move semantics transfer resource ownership instead of copying.
  • T&& is an rvalue reference; std::move() casts to one.
  • Move constructors and move assignment operators avoid expensive deep copies.
  • Follow the Rule of Five when managing resources.
← Previous: Lambda Functions Next: Multithreading →
c++move-semanticsperformanceadvanced
PreviousLesson 43: C++ Lambda FunctionsNextLesson 45: C++ Multithreading
Back to All Tutorials