Imagine you have a box of 1000 books and you want to give it to a friend. One way: buy 1000 new books, place them in a new box, hand it over, then throw your box away. That is a copy followed by a destruction. The sensible way: just hand over the box. That is a move.
Before C++11, C++ often did the silly thing. Returning a big object from a function, inserting a temporary string into a vector, or growing a vector of strings could copy every byte of data and then immediately destroy the original. Move semantics, introduced in C++11, lets an object steal the resources of another object that is about to die anyway. It is one of the main reasons modern C++ is fast, and it is what made move-only types like std::unique_ptr (from Smart Pointers) possible.
This chapter builds on the copy constructor and the Rule of Three from Constructors & Destructors and Operator Overloading.
The Problem: Unnecessary Deep Copies
Consider a class that owns a heap array, like the Buffer from Operator Overloading. Its copy constructor must allocate new memory and copy every element: O(n) work. That is correct when the source will still be used afterwards. But look at these cases:
Buffer b = makeBuffer(); // the returned object is a temporary
v.push_back(Buffer(1000)); // the argument is a temporary
b = Buffer(500); // the right side is a temporaryIn each case the source is a temporary: it will be destroyed at the end of the statement, and nobody can ever look at it again. Copying 1000 ints out of it and then freeing its 1000 ints is pure waste. It would be enough to take its pointer and leave it empty. To do that safely, the compiler must be able to tell "an object that will still be used" apart from "an object that is about to disappear". That distinction is the idea of value categories.
lvalues and rvalues
Every expression in C++ has a type and a value category. Simplified:
An lvalue refers to an object with an identity that persists beyond the expression; you can take its address. Usually: anything with a name.
An rvalue is either a temporary value (a prvalue, "pure rvalue") or an object explicitly marked as expiring (an xvalue, "eXpiring value"). Its resources can be safely reused.
| Expression | Category | Why |
|---|---|---|
x, arr[2], *ptr, "text" | lvalue | Names a persistent object (string literals are lvalue arrays) |
++x, x = 5, function returning T& | lvalue | Refers to an existing object |
42, x + 1, x++, std::string("hi") | prvalue | A fresh temporary value |
Function returning T by value, e.g. makeBuffer() | prvalue | A temporary result |
std::move(x), function returning T&& | xvalue | An existing object marked "you may steal from me" |
expression
/ \
glvalue rvalue
/ \ / \
lvalue xvalue prvalue
(has name) (expiring: (temporary:
std::move) 42, x + 1)
glvalue = "has identity" rvalue = "can be moved from"For everyday programming you only need two rules: if it has a name, it is an lvalue; temporaries and std::move(x) are rvalues.
Rvalue References (T&&)
C++11 added a new kind of reference that binds only to rvalues: the rvalue reference, written T&&. Now we can overload a function on whether its argument is an lvalue or an rvalue:
| Parameter | Binds to lvalue? | Binds to rvalue? |
|---|---|---|
T& | Yes (non-const) | No |
const T& | Yes | Yes (it is the fallback) |
T&& | No | Yes (preferred for rvalues) |
#include <iostream>
#include <string>
#include <utility>
void take(int& x) { std::cout << "int& (lvalue) got " << x << '\n'; }
void take(const int& x) { std::cout << "const int& (const lvalue) got " << x << '\n'; }
void take(int&& x) { std::cout << "int&& (rvalue) got " << x << '\n'; }
std::string makeName() { return "temp"; }
int main() {
int a = 10;
const int c = 20;
take(a); // a has a name and an address: lvalue
take(c); // const lvalue
take(5); // literal: prvalue (a kind of rvalue)
take(a + 1); // result of an expression: prvalue
take(std::move(a)); // std::move(a) is an xvalue (a kind of rvalue)
int&& r = 42; // an rvalue reference binds to a temporary...
take(r); // ...but r itself has a name, so r is an LVALUE
// int& bad = 5; // error: non-const lvalue ref cannot bind to an rvalue
const int& ok = 5; // OK: const lvalue ref binds to anything (lifetime extended)
std::string&& s = makeName(); // OK: temporary's lifetime extended to s
std::cout << ok << ' ' << s << '\n';
}Output:
int& (lvalue) got 10
const int& (const lvalue) got 20
int&& (rvalue) got 5
int&& (rvalue) got 11
int&& (rvalue) got 10
int& (lvalue) got 42
5 tempTwo lines deserve attention:
take(std::move(a))chose theint&&overload.std::moveturned the lvalueainto an xvalue.take(r)choseint&even thoughris declared asint&&. A named rvalue reference is itself an lvalue. The type ofris "rvalue reference to int", but the expressionrhas a name, so its category is lvalue. This rule is deliberate: since you can useragain on the next line, the compiler must not let something steal from it implicitly. It is the single most confusing rule in this chapter, so read it twice.
The last lines show that binding a temporary to a const T& or a T&& extends the temporary's lifetime to that of the reference.
std::move Does Not Move Anything
Despite its name, std::move (header <utility>) performs no work at run time. It is just a cast:
// essentially what the standard library does
template <typename T>
constexpr std::remove_reference_t<T>&& move(T&& t) noexcept {
return static_cast<std::remove_reference_t<T>&&>(t);
}It converts its argument to an xvalue, which means "I, the programmer, promise I do not need this object's current value any more". The actual moving happens later, when overload resolution picks a move constructor or move assignment operator because the argument is now an rvalue. If the class has no move operations, the copy operations are used instead, and nothing is moved at all.
Move Constructor and Move Assignment
A move constructor
T(T&& other)creates a new object by taking over the resources ofother, then leavesotherin a valid state (typically empty).A move assignment operator
T& operator=(T&& other)releases the current object's resources, takes over those ofother, and leavesothervalid.
Here is the full Buffer with all five special members, printing which one runs:
#include <algorithm>
#include <cstddef>
#include <iostream>
#include <utility>
class Buffer {
std::size_t n = 0;
int* data = nullptr;
public:
explicit Buffer(std::size_t size) : n(size), data(new int[size]{}) {
std::cout << " ctor (" << n << " ints allocated)\n";
}
~Buffer() { delete[] data; }
// Copy constructor: allocate and copy every element -- O(n)
Buffer(const Buffer& other) : n(other.n), data(new int[other.n]) {
std::copy(other.data, other.data + n, data);
std::cout << " COPY ctor (" << n << " ints copied)\n";
}
// Move constructor: steal the pointer -- O(1), cannot fail
Buffer(Buffer&& other) noexcept : n(other.n), data(other.data) {
other.n = 0;
other.data = nullptr; // leave the source empty but valid
std::cout << " MOVE ctor (pointer stolen)\n";
}
// Copy assignment (copy-and-swap would also work)
Buffer& operator=(const Buffer& other) {
if (this != &other) {
int* fresh = new int[other.n];
std::copy(other.data, other.data + other.n, fresh);
delete[] data;
data = fresh;
n = other.n;
}
std::cout << " COPY assignment\n";
return *this;
}
// Move assignment: free our memory, steal theirs
Buffer& operator=(Buffer&& other) noexcept {
if (this != &other) {
delete[] data;
data = std::exchange(other.data, nullptr); // C++14: take value, reset source
n = std::exchange(other.n, 0);
}
std::cout << " MOVE assignment\n";
return *this;
}
std::size_t size() const { return n; }
};
Buffer makeBuffer() {
return Buffer(1000); // prvalue: guaranteed copy elision (C++17)
}
int main() {
std::cout << "1. Buffer a(1000)\n";
Buffer a(1000);
std::cout << "2. Buffer b = a\n";
Buffer b = a; // a is an lvalue: copy
std::cout << "3. Buffer c = std::move(a)\n";
Buffer c = std::move(a); // xvalue: move
std::cout << " a.size() = " << a.size() << ", c.size() = " << c.size() << '\n';
std::cout << "4. b = makeBuffer()\n";
b = makeBuffer(); // temporary on the right: move assignment
std::cout << "5. c = b\n";
c = b; // lvalue on the right: copy assignment
std::cout << "end\n";
}Output:
1. Buffer a(1000)
ctor (1000 ints allocated)
2. Buffer b = a
COPY ctor (1000 ints copied)
3. Buffer c = std::move(a)
MOVE ctor (pointer stolen)
a.size() = 0, c.size() = 1000
4. b = makeBuffer()
ctor (1000 ints allocated)
MOVE assignment
5. c = b
COPY assignment
endStep by step:
Buffer b = a;The sourceais an lvalue (we may use it again), so the copy constructor allocates and copies 1000 ints.Buffer c = std::move(a);The source is an xvalue, so the move constructor runs. It copies just a pointer and a size, setsa's pointer tonullptr, and finishes in O(1) regardless of buffer size. That is whya.size()printed 0 afterwards.b = makeBuffer();The function's result is a temporary, so move assignment is chosen automatically, with nostd::moveneeded. InsidemakeBuffer, only one constructor ran (see copy elision below).c = b;bis an lvalue, so copy assignment runs.
Why must the move constructor set other.data = nullptr? Because other's destructor will still run later. If it still held the pointer, it would delete[] memory that c now owns: a double delete. std::exchange(obj, newValue) (C++14) is a handy helper that sets obj to newValue and returns the old value, which is exactly the "take and reset" pattern.
The Moved-From State
After a move, the source object is still alive and its destructor will run. What can you do with it?
For your own classes, you decide. Leaving it "empty" (null pointer, size 0) is the usual choice, and it must at least be safe to destroy and to assign a new value to.
For standard library types, the source is left in a "valid but unspecified state": all operations without preconditions work (
size(),clear(), assignment, destruction), but you must not assume any particular value. A moved-fromstd::stringis empty in practice, but the standard does not promise it. A few types do make a guarantee: a moved-fromstd::unique_ptrorstd::shared_ptris always null.
#include <iostream>
#include <memory>
#include <string>
#include <utility>
#include <vector>
class Student {
std::string name;
public:
// Sink parameter: take by value, then move into the member
explicit Student(std::string n) : name(std::move(n)) {}
const std::string& getName() const { return name; }
};
int main() {
std::string s = "Meenakshi Sundaram";
Student a(s); // s copied into the parameter, then moved into name
Student b(std::move(s)); // moved into the parameter, then moved into name
std::cout << a.getName() << " | " << b.getName() << '\n';
std::cout << "s after move: \"" << s << "\" (valid but unspecified; here empty)\n";
s = "reused"; // assigning a new value to a moved-from object is fine
std::cout << "s reassigned: " << s << '\n';
// std::move on a const object silently copies
const std::string fixed = "constant text";
std::string t = std::move(fixed); // calls the COPY constructor
std::cout << "fixed still: " << fixed << '\n';
// vector of move-only objects
std::vector<std::unique_ptr<int>> v;
auto p = std::make_unique<int>(7);
v.push_back(std::move(p)); // push_back(p) would not compile
std::cout << "v[0] = " << *v[0] << ", p is " << (p ? "set" : "null") << '\n';
// init capture moves a move-only object into a lambda (C++14)
auto q = std::make_unique<int>(99);
auto show = [ptr = std::move(q)] { std::cout << "lambda owns " << *ptr << '\n'; };
show();
}Output:
Meenakshi Sundaram | Meenakshi Sundaram
s after move: "" (valid but unspecified; here empty)
s reassigned: reused
fixed still: constant text
v[0] = 7, p is null
lambda owns 99This program shows several practical patterns:
Sink parameters: a constructor that will store a string takes it by value and then moves it into the member. Callers with an lvalue pay one copy (unavoidable, they keep their string) plus a cheap move; callers with an rvalue pay only moves. One function serves both cases well.
std::moveon aconstobject copies.std::move(fixed)producesconst std::string&&, which cannot bind to the move constructor'sstd::string&&(that would allow modifying a const object), so the copy constructor'sconst std::string&is chosen. No error, no warning, just a silent copy.Move-only types such as
std::unique_ptr,std::threadand file streams can be put into containers and lambdas only by moving. The init capture[ptr = std::move(q)]is the C++14 way to move into a lambda, as promised in Lambda Expressions.
noexcept Matters: Why vector Sometimes Copies
When a vector grows, it allocates a bigger buffer and transfers the old elements. Recall the strong exception guarantee from Exception Handling: push_back promises that if an exception is thrown, the vector is left unchanged. With copying that is easy: if copy number 5 throws, throw away the new buffer, and the old one is untouched. With moving, elements 1 to 4 have already been gutted, so the old state is lost.
Therefore vector moves elements during reallocation only if the move constructor is noexcept (or if the type cannot be copied at all). Otherwise it copies. Watch this:
#include <iostream>
#include <vector>
struct Safe {
Safe() = default;
Safe(const Safe&) { std::cout << "C"; }
Safe(Safe&&) noexcept { std::cout << "M"; } // promises not to throw
};
struct Risky {
Risky() = default;
Risky(const Risky&) { std::cout << "C"; }
Risky(Risky&&) { std::cout << "M"; } // no noexcept
};
int main() {
std::vector<Safe> s;
std::cout << "Safe : ";
for (int i = 0; i < 5; ++i) { s.emplace_back(); std::cout << '|'; }
std::cout << '\n';
std::vector<Risky> r;
std::cout << "Risky: ";
for (int i = 0; i < 5; ++i) { r.emplace_back(); std::cout << '|'; }
std::cout << '\n';
}Output (GCC 15; each | marks the end of one emplace_back):
Safe : |M|MM||MMMM|
Risky: |C|CC||CCCC|The capacity grew 1 → 2 → 4 → 8, so reallocations transferred 1, 2 and 4 existing elements. Safe was moved each time, Risky was copied each time, even though it has a perfectly good move constructor. For a vector of large objects that is a huge performance difference. Rule: mark move constructors and move assignment noexcept whenever they cannot throw, which is almost always, since they usually just swap pointers. (Internally the library uses std::move_if_noexcept for this decision.)
When Does the Compiler Generate Move Operations?
The compiler can write move operations for you: the implicit move constructor moves each member in turn. But the rules are subtle:
The move constructor and move assignment are implicitly declared only if the class has no user-declared copy constructor, copy assignment operator, destructor, or the other move operation.
If a class declares a move constructor or move assignment, its implicit copy constructor and copy assignment are defined as deleted.
The danger is in the first rule. Adding just a destructor (maybe for logging) removes the implicit moves, and std::move then silently falls back to copying:
#include <iostream>
#include <utility>
struct Tracer {
Tracer() = default;
Tracer(const Tracer&) { std::cout << "copied\n"; }
Tracer(Tracer&&) noexcept { std::cout << "moved\n"; }
};
struct RuleOfZero { // declares no special members at all
Tracer t;
};
struct WithDestructor { // only adds a destructor...
Tracer t;
~WithDestructor() {} // ...which silently removes the implicit move operations
};
struct Fixed { // destructor + explicitly defaulted moves (and copies)
Tracer t;
~Fixed() {}
Fixed() = default;
Fixed(const Fixed&) = default;
Fixed(Fixed&&) = default;
Fixed& operator=(const Fixed&) = default;
Fixed& operator=(Fixed&&) = default;
};
int main() {
RuleOfZero a;
std::cout << "RuleOfZero: ";
[[maybe_unused]] RuleOfZero a2 = std::move(a);
WithDestructor b;
std::cout << "WithDestructor: ";
[[maybe_unused]] WithDestructor b2 = std::move(b); // falls back to the copy constructor!
Fixed c;
std::cout << "Fixed: ";
[[maybe_unused]] Fixed c2 = std::move(c);
}Output:
RuleOfZero: moved
WithDestructor: copied
Fixed: movedThe program still compiles and runs correctly; it is just slower. Nothing warns you. This is why the rules of thumb exist:
| Rule | Statement |
|---|---|
| Rule of Zero | Best: don't declare any of the five special members. Let members like std::string, std::vector and std::unique_ptr manage resources; the compiler-generated copy and move then do the right thing automatically. |
| Rule of Five | If you must declare any of destructor, copy constructor, copy assignment, move constructor, move assignment (typically because the class directly owns a raw resource), declare all five, using = default or = delete where appropriate. |
| Rule of Three (pre-C++11) | Destructor, copy constructor, copy assignment go together. Still true, but without the two move operations such a class can never be moved, only copied. |
The Fixed struct shows the standard way out: if you need a destructor, explicitly = default the other four. Note that a class with a user-declared destructor still gets implicit copy operations, but that is deprecated and only kept for compatibility.
Copy Elision and Returning by Value
Since we can move, is returning a big object from a function expensive? Usually it costs nothing, thanks to copy elision: the compiler constructs the result directly in the caller's variable.
#include <iostream>
struct Tracer {
Tracer() { std::cout << " constructed\n"; }
Tracer(const Tracer&) { std::cout << " copied\n"; }
Tracer(Tracer&&) noexcept { std::cout << " moved\n"; }
};
Tracer makePrvalue() {
return Tracer(); // C++17: guaranteed elision, no copy or move ever
}
Tracer makeNamed() {
Tracer t;
return t; // NRVO: allowed (GCC does it), not guaranteed
}
Tracer makeNamedTwoPaths(bool flag) {
Tracer x, y;
if (flag) return x; // two candidates: NRVO not possible here,
return y; // but return still MOVES automatically
}
int main() {
std::cout << "makePrvalue:\n";
[[maybe_unused]] Tracer a = makePrvalue();
std::cout << "makeNamed:\n";
[[maybe_unused]] Tracer b = makeNamed();
std::cout << "makeNamedTwoPaths:\n";
[[maybe_unused]] Tracer c = makeNamedTwoPaths(true);
}Output (GCC 15):
makePrvalue:
constructed
makeNamed:
constructed
makeNamedTwoPaths:
constructed
constructed
movedReturning a prvalue (
return Tracer();) gets guaranteed copy elision since C++17. No copy or move constructor is even needed; it works for types that are neither copyable nor movable. This is often called RVO (return value optimization).Returning a named local (
return t;) may get NRVO (named RVO). It is permitted but not required. GCC and Clang do it in simple cases like this one, even without optimization flags (our build used none).When NRVO is impossible (here, two different locals can be returned), the compiler still automatically treats the returned local as an rvalue and moves it. You never get a copy for a plain
return local;of a movable type. (C++20 and C++23 extended this "implicit move" to more situations, such as returning rvalue reference parameters.)
So you should not write return std::move(local);. It turns the return expression into an xvalue, which disables NRVO and forces a move:
#include <iostream>
#include <utility>
struct Tracer {
Tracer() { std::cout << " constructed\n"; }
Tracer(const Tracer&) { std::cout << " copied\n"; }
Tracer(Tracer&&) noexcept { std::cout << " moved\n"; }
};
Tracer makeNamed() {
Tracer t;
return std::move(t); // "optimization" that blocks NRVO
}
int main() {
[[maybe_unused]] Tracer b = makeNamed();
}Output:
constructed
movedGCC even warns about it with -Wall:
warning: moving a local object in a return statement prevents copy elision [-Wpessimizing-move]
note: remove ‘std::move’ callForwarding References and std::forward
There is one place where T&& does not mean "rvalue reference": when T is a template parameter being deduced (and in auto&&). Then it is a forwarding reference (also called a universal reference) and it binds to anything:
Called with an lvalue of type
std::string:Tis deduced asstd::string&, and the parameter typestd::string& &&collapses tostd::string&.Called with an rvalue:
Tis deduced asstd::string, and the parameter isstd::string&&.
| Reference collapsing | Result |
|---|---|
T& &, T& &&, T&& & | T& (any lvalue reference wins) |
T&& && | T&& |
Inside the function, the parameter has a name, so it is an lvalue either way. To pass the argument on with its original category, use std::forward<T>(arg): it casts to an rvalue only if the original argument was an rvalue. This is called perfect forwarding.
#include <iostream>
#include <string>
#include <utility>
void process(const std::string& s) { std::cout << "copy path for \"" << s << "\"\n"; }
void process(std::string&& s) { std::cout << "move path for \"" << s << "\"\n"; }
// T&& here is a FORWARDING reference: T is deduced from the argument
template <typename T>
void wrapperNoForward(T&& arg) {
process(arg); // arg has a name: always an lvalue
}
template <typename T>
void wrapperForward(T&& arg) {
process(std::forward<T>(arg)); // restores the original value category
}
int main() {
std::string name = "Asha";
std::cout << "Without std::forward:\n";
wrapperNoForward(name); // T = std::string&
wrapperNoForward(std::string("Ravi")); // T = std::string
std::cout << "With std::forward:\n";
wrapperForward(name);
wrapperForward(std::string("Ravi"));
}Output:
Without std::forward:
copy path for "Asha"
copy path for "Ravi"
With std::forward:
copy path for "Asha"
move path for "Ravi"Without std::forward, the temporary "Ravi" was copied, because inside the wrapper it became the named lvalue arg. With it, the lvalue name was still copied (correct: the caller still owns it), and the temporary was moved. This is exactly how std::make_unique, std::make_shared and emplace_back pass your constructor arguments through to the real constructor without extra copies.
Simple rule: use std::move on rvalue references, std::forward on forwarding references.
Copy vs Move at a Glance
| Copy | Move | |
|---|---|---|
| Chosen when the source is | an lvalue | an rvalue (temporary or std::moved) |
| Signature | T(const T&), T& operator=(const T&) | T(T&&) noexcept, T& operator=(T&&) noexcept |
| Source afterwards | Unchanged | Valid but (usually) empty / unspecified |
| Cost for resource-owning types | O(n): allocate and copy | O(1): transfer pointers |
| Can throw? | Often (allocation) | Should not: mark noexcept |
For int, double, plain structs | Copies the bytes | Also copies the bytes: moving a scalar is a copy |
The last row matters: moving only helps types that own something through a pointer or handle. std::move on an int or an std::array<int, 1000> copies the same bytes.
Common Mistakes
1. Using an object after moving from it
After v2 = std::move(v1);, do not read v1's contents. Assign it a new value or let it be destroyed.
2. return std::move(local)
Prevents NRVO. Write return local;; the compiler moves automatically when it cannot elide.
3. Forgetting noexcept on move operations
Your type will be copied instead of moved when a vector reallocates.
4. Adding a destructor and losing moves
A user-declared destructor (or copy operation) suppresses the implicit move operations. Follow the Rule of Zero or the Rule of Five.
5. Not resetting the source in a hand-written move
If the move constructor copies the pointer but leaves other.data unchanged, both destructors free the same memory: double delete.
6. std::move on const objects
It compiles, but copies. Don't make variables const if you plan to move from them.
7. Thinking T&& in a template is always an rvalue reference
In template<typename T> void f(T&&), it is a forwarding reference. Using std::move on it would steal from the caller's lvalues; use std::forward<T>.
Interview / GATE-Style Questions
Q1. What does std::move actually do?
Nothing at run time. It is a cast to an rvalue reference (an xvalue), equivalent to static_cast<T&&>(x). It makes the object eligible to be bound by a move constructor or move assignment operator; those functions do the actual moving. If the type has no move operations, the copy operations are used.
Q2. Inside void f(std::string&& s), is the expression s an lvalue or an rvalue? How do you move from it?
It is an lvalue, because it has a name, even though its declared type is an rvalue reference. To move from it, write std::move(s), e.g. member = std::move(s);.
Q3. Why should move constructors be noexcept?
Containers like std::vector offer the strong exception guarantee when reallocating. They can move elements only if moving cannot throw; otherwise a throw midway would leave the old elements already moved-from. So vector uses std::move_if_noexcept and falls back to copying if the move constructor is not noexcept (and the type is copyable).
Q4. A class declares only a destructor. Does it get a move constructor? What happens with std::move on it?
No. A user-declared destructor prevents the implicit declaration of the move constructor and move assignment. std::move(obj) then binds to the implicitly generated (deprecated but still present) copy constructor taking const T&, so the object is copied.
Q5. What is the difference between RVO and NRVO? Which is guaranteed?
RVO applies when a function returns a prvalue (e.g. return T(args);); since C++17 this elision is guaranteed and needs no accessible copy or move constructor. NRVO applies when returning a named local object; it is allowed but optional. If NRVO is not performed, the local is implicitly moved (treated as an rvalue) in the return statement.
Q6. What is printed? std::string a = "hi"; std::string b = std::move(a); std::cout << b.size();
2. b took over the contents. (Printing a would show an unspecified, though valid, value; with GCC it is empty.)
Q7. In template<class T> void g(T&& x), what is T when g is called with an int lvalue? With the literal 5?
With an lvalue int: T = int&, and the parameter type int& && collapses to int&. With 5: T = int, and the parameter type is int&&.
Summary
Lvalues have identity (a name); rvalues are temporaries (prvalues) or objects marked as expiring (xvalues).
T&&binds to rvalues; a named rvalue reference is an lvalue.std::moveis only a cast; move constructors and move assignment operators do the real work by stealing resources and leaving the source valid.Mark moves
noexcept, orvectorwill copy during reallocation.Implicit moves disappear when you declare a destructor or copy operation: prefer the Rule of Zero, otherwise follow the Rule of Five.
Return locals by value with plain
return x;: copy elision (guaranteed for prvalues since C++17) or implicit move makes it cheap.Forwarding references (
T&&with deducedT) plusstd::forwardgive perfect forwarding.
Next, in File Handling, we leave memory behind and learn how C++ programs read and write files, which, as you will see, are themselves move-only RAII objects.