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 // ADDITIONAL_COMPILE_FLAGS: -fno-exceptions
10 
11 // UNSUPPORTED: c++03
12 
13 // <vector>
14 
15 // Test that vector always moves elements when exceptions are disabled.
16 // vector is allowed to move or copy elements while resizing, so long as
17 // it still provides the strong exception safety guarantee.
18 
19 #include <vector>
20 #include <cassert>
21 
22 #include "test_macros.h"
23 
24 #ifndef TEST_HAS_NO_EXCEPTIONS
25 #error exceptions should be disabled.
26 #endif
27 
28 bool allow_moves = false;
29 
30 class A {
31 public:
A()32   A() {}
A(A &&)33   A(A&&) { assert(allow_moves); }
A(int)34   explicit A(int) {}
A(A const &)35   A(A const&) { assert(false); }
36 };
37 
main(int,char **)38 int main(int, char**) {
39   std::vector<A> v;
40 
41   // Create a vector containing some number of elements that will
42   // have to be moved when it is resized.
43   v.reserve(10);
44   std::size_t old_cap = v.capacity();
45   for (std::size_t i = 0; i < v.capacity(); ++i) {
46     v.emplace_back(42);
47   }
48   assert(v.capacity() == old_cap);
49   assert(v.size() == v.capacity());
50 
51   // The next emplace back should resize.
52   allow_moves = true;
53   v.emplace_back(42);
54 
55   return 0;
56 }
57