-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmove-cpp11.cpp
More file actions
62 lines (45 loc) · 1.85 KB
/
move-cpp11.cpp
File metadata and controls
62 lines (45 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#pragma clang diagnostic ignored "-Wunused-variable"
#pragma clang diagnostic ignored "-Wunused-value"
#pragma clang diagnostic ignored "-Wunused-function"
#define CATCH_CONFIG_MAIN // Tells Catch2 to provide a main()
#include "../catch/catch_amalgamated.hpp"
#include "utils.h"
using namespace std;
namespace {
// std::move()
// - std::move does NOT move — it just casts to rvalue reference
// - Actual move happens in the move constructor / move assignment operator
//
// Key notes:
// - After move: object is valid but in unspecified state/value — don't use it
// - Don't move return values — NRVO is better, move prevents it
// - std::move is free — zero runtime cost, just a cast
// - Types without move semantics fall back to copy silently !!
//// move in function return — unnecessary (NRVO does this automatically)
std::vector<int> make() {
std::vector<int> v = {1,2,3};
// return std::move(v); // ❌ actually prevents NRVO — don't do this
return v; // ✓ compiler moves automatically
}
TEST_CASE("move-1") {
//// main use case
// without move — expensive copy
std::vector<int> a = {1,2,3,4,5};
std::vector<int> b = a; // copies all elements — a still valid
// with move — cheap transfer
std::vector<int> a_ = {1,2,3,4,5};
std::vector<int> b_ = std::move(a_); // steals a's buffer — no copy
// a is now empty — valid but unspecified state/value
//// move into container — avoid copy
std::vector<std::string> names;
std::string s = "hello";
names.push_back(s); // copies s
names.push_back(std::move(s)); // moves s — s now empty ✓
//// move in class — move constructor
struct Buffer {
std::vector<int> data;
Buffer(Buffer&& other) : data(std::move(other.data)) {} // ✓
};
// REQUIRE(arr[0] == 42);
}
}