|
| 1 | +// noexcept_sort_vs_realloc.cpp -- 验证 noexcept 对 std::sort 和 vector 扩容的影响 |
| 2 | +// Standard: C++17 |
| 3 | +// 对应文档:vol2-modern-features/ch00-move-semantics/05-move-in-practice.md |
| 4 | +// |
| 5 | +// 核心结论: |
| 6 | +// - std::sort 只用移动,不区分移动操作是否 noexcept(两种类型都是 拷贝=0) |
| 7 | +// - vector 扩容通过 move_if_noexcept 选择策略:noexcept 类型用移动, |
| 8 | +// 非 noexcept 类型退回拷贝(强异常安全) |
| 9 | +#include <algorithm> |
| 10 | +#include <iostream> |
| 11 | +#include <string> |
| 12 | +#include <vector> |
| 13 | + |
| 14 | +// 移动操作带 noexcept 的类型 |
| 15 | +struct NoexceptType { |
| 16 | + std::string payload; |
| 17 | + int value; |
| 18 | + |
| 19 | + static int copy_count; |
| 20 | + static int move_count; |
| 21 | + |
| 22 | + NoexceptType(int v) : payload("data"), value(v) {} |
| 23 | + NoexceptType(const NoexceptType& o) : payload(o.payload + "_c"), value(o.value) { |
| 24 | + ++copy_count; |
| 25 | + } |
| 26 | + NoexceptType(NoexceptType&& o) noexcept : payload(std::move(o.payload)), value(o.value) { |
| 27 | + o.payload = "(moved)"; |
| 28 | + ++move_count; |
| 29 | + } |
| 30 | + NoexceptType& operator=(NoexceptType&& o) noexcept { |
| 31 | + payload = std::move(o.payload); |
| 32 | + value = o.value; |
| 33 | + o.payload = "(moved)"; |
| 34 | + ++move_count; |
| 35 | + return *this; |
| 36 | + } |
| 37 | + NoexceptType& operator=(const NoexceptType& o) { |
| 38 | + payload = o.payload + "_c"; |
| 39 | + value = o.value; |
| 40 | + ++copy_count; |
| 41 | + return *this; |
| 42 | + } |
| 43 | + bool operator<(const NoexceptType& rhs) const { return value < rhs.value; } |
| 44 | + static void reset() { |
| 45 | + copy_count = 0; |
| 46 | + move_count = 0; |
| 47 | + } |
| 48 | +}; |
| 49 | + |
| 50 | +// ThrowingType 与 NoexceptType 完全相同,唯一区别是移动操作没有 noexcept |
| 51 | +struct ThrowingType { |
| 52 | + std::string payload; |
| 53 | + int value; |
| 54 | + |
| 55 | + static int copy_count; |
| 56 | + static int move_count; |
| 57 | + |
| 58 | + ThrowingType(int v) : payload("data"), value(v) {} |
| 59 | + ThrowingType(const ThrowingType& o) : payload(o.payload + "_c"), value(o.value) { |
| 60 | + ++copy_count; |
| 61 | + } |
| 62 | + ThrowingType(ThrowingType&& o) // 注意:没有 noexcept |
| 63 | + : payload(std::move(o.payload)), value(o.value) { |
| 64 | + o.payload = "(moved)"; |
| 65 | + ++move_count; |
| 66 | + } |
| 67 | + ThrowingType& operator=(ThrowingType&& o) // 注意:没有 noexcept |
| 68 | + { |
| 69 | + payload = std::move(o.payload); |
| 70 | + value = o.value; |
| 71 | + o.payload = "(moved)"; |
| 72 | + ++move_count; |
| 73 | + return *this; |
| 74 | + } |
| 75 | + ThrowingType& operator=(const ThrowingType& o) { |
| 76 | + payload = o.payload + "_c"; |
| 77 | + value = o.value; |
| 78 | + ++copy_count; |
| 79 | + return *this; |
| 80 | + } |
| 81 | + bool operator<(const ThrowingType& rhs) const { return value < rhs.value; } |
| 82 | + static void reset() { |
| 83 | + copy_count = 0; |
| 84 | + move_count = 0; |
| 85 | + } |
| 86 | +}; |
| 87 | + |
| 88 | +int NoexceptType::copy_count = 0; |
| 89 | +int NoexceptType::move_count = 0; |
| 90 | +int ThrowingType::copy_count = 0; |
| 91 | +int ThrowingType::move_count = 0; |
| 92 | + |
| 93 | +int main() { |
| 94 | + const int kCount = 5000; |
| 95 | + |
| 96 | + // Test 1: std::sort(noexcept 类型) |
| 97 | + { |
| 98 | + std::vector<NoexceptType> vec; |
| 99 | + vec.reserve(kCount); |
| 100 | + for (int i = 0; i < kCount; ++i) |
| 101 | + vec.emplace_back(kCount - i); |
| 102 | + NoexceptType::reset(); |
| 103 | + std::sort(vec.begin(), vec.end()); |
| 104 | + std::cout << "noexcept sort: 拷贝=" << NoexceptType::copy_count |
| 105 | + << " 移动=" << NoexceptType::move_count << "\n"; |
| 106 | + } |
| 107 | + |
| 108 | + // Test 2: std::sort(非 noexcept 类型) |
| 109 | + { |
| 110 | + std::vector<ThrowingType> vec; |
| 111 | + vec.reserve(kCount); |
| 112 | + for (int i = 0; i < kCount; ++i) |
| 113 | + vec.emplace_back(kCount - i); |
| 114 | + ThrowingType::reset(); |
| 115 | + std::sort(vec.begin(), vec.end()); |
| 116 | + std::cout << "非noexcept sort: 拷贝=" << ThrowingType::copy_count |
| 117 | + << " 移动=" << ThrowingType::move_count << "\n"; |
| 118 | + } |
| 119 | + |
| 120 | + std::cout << "\n"; |
| 121 | + |
| 122 | + // Test 3: vector 扩容(noexcept 类型,无 reserve) |
| 123 | + { |
| 124 | + NoexceptType::reset(); |
| 125 | + std::vector<NoexceptType> vec; |
| 126 | + for (int i = 0; i < 200; ++i) |
| 127 | + vec.emplace_back(i); |
| 128 | + std::cout << "noexcept 扩容: 拷贝=" << NoexceptType::copy_count |
| 129 | + << " 移动=" << NoexceptType::move_count << "\n"; |
| 130 | + } |
| 131 | + |
| 132 | + // Test 4: vector 扩容(非 noexcept 类型,无 reserve) |
| 133 | + // ThrowingType 的扩容会退回拷贝,因为 move_if_noexcept 不选中它的移动 |
| 134 | + { |
| 135 | + ThrowingType::reset(); |
| 136 | + std::vector<ThrowingType> vec; |
| 137 | + for (int i = 0; i < 200; ++i) |
| 138 | + vec.emplace_back(i); |
| 139 | + std::cout << "非noexcept扩容: 拷贝=" << ThrowingType::copy_count |
| 140 | + << " 移动=" << ThrowingType::move_count << "\n"; |
| 141 | + } |
| 142 | +} |
0 commit comments