5.2.9 · HinglishC++ Programming

Move semantics — rvalue references (&&), std - move, std - forward

2,146 words10 min readRead in English

5.2.9 · Coding › C++ Programming


WHY karta hai yeh exist?


WHAT hai ek rvalue reference?

Figure — Move semantics — rvalue references (&&), std - move, std - forward

HOW kaam karta hai ek move constructor? (Scratch se derive karo)

Imagine karo ek chhoti si string class jo ek heap buffer own karti hai.

class Str {
    char* data;      // owns heap memory
    size_t len;
public:
    // Copy constructor: DEEP copy (expensive)
    Str(const Str& o) : len(o.len) {
        data = new char[len+1];          // allocate
        std::memcpy(data, o.data, len+1);// copy all bytes
    }                                    // o is left untouched
 
    // Move constructor: STEAL (cheap)
    Str(Str&& o) noexcept
        : data(o.data), len(o.len) {     // grab his pointer
        o.data = nullptr;                // leave source EMPTY but valid
        o.len  = 0;
    }
    ~Str() { delete[] data; }            // safe: nullptr delete is no-op
};

WHAT hai std::move?


WHY chahiye std::forward? (Perfect forwarding)


std::move vs std::forward


Common Mistakes (Steel-manned)


Worked example: copy + move wali ek class


Recall Feynman: 12-saal ke bacche ko explain karo

Imagine karo tum ghar shift kar rahe ho. Copying matlab hai apna sara purana furniture bilkul ek jaisa naaya kharidna — slow aur wasteful. Moving matlab hai apna existing furniture seedha naaye ghar le jaana. && truck par ek sign hai jo kehta hai "yeh furniture lene ke liye available hai, purana ghar girayaa ja raha hai." std::move matlab hai tum khud apne furniture par woh sign lagao yeh kehne ke liye "main isse done ho gaya, le lo." std::forward ek delivery wala hai jo sign ko bilkul waisa hi rakhta hai jaise tha, taaki agla banda jaane ki copy karna hai ya asli cheez leni hai.


Flashcards

std::move(x) runtime par actually kya karta hai?
Kuch nahi — yeh ek compile-time static_cast<T&&>(x) hai. Yeh sirf move constructor/assignment select karne ko enable karta hai.
T&& (non-template) parameter se kya bind hota hai?
Sirf rvalues (temporaries); lvalues nahi.
Moved-from object ko reset kyun karna chahiye (jaise pointer ko null set karna)?
Double-free / double-delete se bachne ke liye; use valid-but-unspecified chhoda jaana chahiye taaki uska destructor safe ho.
Move constructor ko noexcept kyun mark karein?
Taaki std::vector (aur dusre) reallocation ke dauran isse use karein; warna exception safety ke liye woh copying par fall back karte hain.
std::move aur std::forward mein fark?
move ek unconditional cast to rvalue hai; forward<T> ek conditional cast hai jo caller ki lvalue/rvalue category preserve karta hai, sirf forwarding references ke saath use hota hai.
Forwarding (universal) reference kya hota hai?
Ek T&& jahan T ek deduced template parameter hai; yeh reference collapsing ke through dono lvalues aur rvalues se bind hota hai.
Reference collapsing ke rules?
T& &, T& &&, T&& & sab → T&; sirf T&& &&T&&.
return std::move(localVar); zyaadatar buri idea kyun hai?
Yeh copy elision/NRVO disable kar deta hai; plain return localVar; already move karta hai ya elide karta hai aur kam se kam utna hi fast hota hai.
void f(Str&& s) ke andar, s lvalue hai ya rvalue?
Ek lvalue (uska naam hai). Isse move karne ke liye aapko std::move(s) karna hoga.
Agar kisi type mein move constructor nahi hai, toh std::move(x) kya select karta hai?
Copy constructor — silently, bina kisi error ke.

Connections

  • Rule of Five — destructor, copy ctor, copy assign, move ctor, move assign
  • RAII and Ownership — moves resources ki ownership transfer karte hain
  • Copy elision and RVO — jab compiler construction bilkul skip kar deta hai
  • Templates and type deduction — forwarding references ka basis
  • std::vector internals — reallocation ke dauran moves use karta hai
  • Reference collapsing — woh rule jo forwarding ko kaam karta hai
  • noexcept specifier

Concept Map

motivates

transfer ownership

contrast

binds to

enables

grabs pointer then

prevents

casts lvalue to

overload vs const T&

lets vector use move

preserves value category

Deep copy of temporaries wastes work

Move semantics

Steal heap buffer

lvalue: named, has address

rvalue: temporary, expiring

rvalue reference T&&

Move constructor

Reset source to nullptr

double delete UB

std - move

Compiler picks move or copy

noexcept

std - forward