blob: 6f36473fa989b46c277cffbceeb095fb3bca3e66 [file] [log] [blame]
Howard Hinnantd1d27a42011-06-03 19:40:40 +00001//===----------------------------------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10// <vector>
11
12// void swap(vector& c)
13// noexcept(!allocator_type::propagate_on_container_swap::value ||
14// __is_nothrow_swappable<allocator_type>::value);
Marshall Clow7d914d12015-07-13 20:04:56 +000015//
16// In C++17, the standard says that swap shall have:
17// noexcept(allocator_traits<Allocator>::propagate_on_container_swap::value ||
18// allocator_traits<Allocator>::is_always_equal::value);
Howard Hinnantd1d27a42011-06-03 19:40:40 +000019
20// This tests a conforming extension
21
22#include <vector>
23#include <cassert>
24
Marshall Clow1b921882013-12-03 00:18:10 +000025#include "test_allocator.h"
Howard Hinnantd1d27a42011-06-03 19:40:40 +000026
27template <class T>
28struct some_alloc
29{
30 typedef T value_type;
31
32 some_alloc() {}
33 some_alloc(const some_alloc&);
34 void deallocate(void*, unsigned) {}
35
36 typedef std::true_type propagate_on_container_swap;
37};
38
Marshall Clow7d914d12015-07-13 20:04:56 +000039template <class T>
40struct some_alloc2
41{
42 typedef T value_type;
43
44 some_alloc2() {}
45 some_alloc2(const some_alloc2&);
46 void deallocate(void*, unsigned) {}
47
48 typedef std::false_type propagate_on_container_swap;
49 typedef std::true_type is_always_equal;
50};
51
Howard Hinnantd1d27a42011-06-03 19:40:40 +000052int main()
53{
Dan Albert1d4a1ed2016-05-25 22:36:09 -070054#if __has_feature(cxx_noexcept)
Howard Hinnantd1d27a42011-06-03 19:40:40 +000055 {
56 typedef std::vector<bool> C;
57 C c1, c2;
58 static_assert(noexcept(swap(c1, c2)), "");
59 }
60 {
61 typedef std::vector<bool, test_allocator<bool>> C;
62 C c1, c2;
63 static_assert(noexcept(swap(c1, c2)), "");
64 }
65 {
66 typedef std::vector<bool, other_allocator<bool>> C;
67 C c1, c2;
68 static_assert(noexcept(swap(c1, c2)), "");
69 }
70 {
71 typedef std::vector<bool, some_alloc<bool>> C;
72 C c1, c2;
Marshall Clow7d914d12015-07-13 20:04:56 +000073#if TEST_STD_VER >= 14
74 // In c++14, if POCS is set, swapping the allocator is required not to throw
75 static_assert( noexcept(swap(c1, c2)), "");
76#else
Howard Hinnantd1d27a42011-06-03 19:40:40 +000077 static_assert(!noexcept(swap(c1, c2)), "");
Marshall Clow7d914d12015-07-13 20:04:56 +000078#endif
Howard Hinnantd1d27a42011-06-03 19:40:40 +000079 }
Marshall Clow7d914d12015-07-13 20:04:56 +000080#if TEST_STD_VER >= 14
81 {
82 typedef std::vector<bool, some_alloc2<bool>> C;
83 C c1, c2;
84 // if the allocators are always equal, then the swap can be noexcept
85 static_assert( noexcept(swap(c1, c2)), "");
86 }
87#endif
Dan Albert1d4a1ed2016-05-25 22:36:09 -070088
89#endif
Howard Hinnantd1d27a42011-06-03 19:40:40 +000090}