1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
10// UNSUPPORTED: no-exceptions
11
12// <flat_map>
13
14// flat_map(flat_map&& s);
15// If any member function in [flat.map.defn] exits via an exception, the invariant is restored.
16
17#include <algorithm>
18#include <cassert>
19#include <flat_map>
20#include <functional>
21#include <utility>
22#include <vector>
23
24#include "../helpers.h"
25#include "test_macros.h"
26
27static int countdown = 0;
28
29struct EvilContainer : std::vector<int> {
30 EvilContainer() = default;
31 EvilContainer(EvilContainer&& rhs) {
32 // Throw on move-construction.
33 if (--countdown == 0) {
34 rhs.insert(rhs.end(), 0);
35 rhs.insert(rhs.end(), 0);
36 throw 42;
37 }
38 }
39};
40
41int main(int, char**) {
42 {
43 using M = std::flat_map<int, int, std::less<int>, EvilContainer, std::vector<int>>;
44 M mo = {{1, 1}, {2, 2}, {3, 3}};
45 countdown = 1;
46 try {
47 M m = std::move(mo);
48 assert(false); // not reached
49 } catch (int x) {
50 assert(x == 42);
51 }
52 // The source flat_map maintains its class invariant.
53 check_invariant(mo);
54 LIBCPP_ASSERT(mo.empty());
55 }
56 {
57 using M = std::flat_map<int, int, std::less<int>, std::vector<int>, EvilContainer>;
58 M mo = {{1, 1}, {2, 2}, {3, 3}};
59 countdown = 1;
60 try {
61 M m = std::move(mo);
62 assert(false); // not reached
63 } catch (int x) {
64 assert(x == 42);
65 }
66 // The source flat_map maintains its class invariant.
67 check_invariant(mo);
68 LIBCPP_ASSERT(mo.empty());
69 }
70 return 0;
71}
72

source code of libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/move_exceptions.pass.cpp