blob: 1d00ff387480ca4c4075f4e4d8397928f3c07122 [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 Clowdf00d5e2015-01-28 21:22:53 +000025#include "MoveOnly.h"
Marshall Clow1b921882013-12-03 00:18:10 +000026#include "test_allocator.h"
Howard Hinnantd1d27a42011-06-03 19:40:40 +000027
28template <class T>
29struct some_alloc
30{
31 typedef T value_type;
32
33 some_alloc() {}
34 some_alloc(const some_alloc&);
35 void deallocate(void*, unsigned) {}
36
37 typedef std::true_type propagate_on_container_swap;
38};
39
Marshall Clow7d914d12015-07-13 20:04:56 +000040template <class T>
41struct some_alloc2
42{
43 typedef T value_type;
44
45 some_alloc2() {}
46 some_alloc2(const some_alloc2&);
47 void deallocate(void*, unsigned) {}
48
49 typedef std::false_type propagate_on_container_swap;
50 typedef std::true_type is_always_equal;
51};
52
Howard Hinnantd1d27a42011-06-03 19:40:40 +000053int main()
54{
Dan Albert1d4a1ed2016-05-25 22:36:09 -070055#if __has_feature(cxx_noexcept)
Howard Hinnantd1d27a42011-06-03 19:40:40 +000056 {
57 typedef std::vector<MoveOnly> C;
58 C c1, c2;
59 static_assert(noexcept(swap(c1, c2)), "");
60 }
61 {
62 typedef std::vector<MoveOnly, test_allocator<MoveOnly>> C;
63 C c1, c2;
64 static_assert(noexcept(swap(c1, c2)), "");
65 }
66 {
67 typedef std::vector<MoveOnly, other_allocator<MoveOnly>> C;
68 C c1, c2;
69 static_assert(noexcept(swap(c1, c2)), "");
70 }
71 {
72 typedef std::vector<MoveOnly, some_alloc<MoveOnly>> C;
73 C c1, c2;
Marshall Clow7d914d12015-07-13 20:04:56 +000074#if TEST_STD_VER >= 14
75 // In c++14, if POCS is set, swapping the allocator is required not to throw
76 static_assert( noexcept(swap(c1, c2)), "");
77#else
Howard Hinnantd1d27a42011-06-03 19:40:40 +000078 static_assert(!noexcept(swap(c1, c2)), "");
Marshall Clow7d914d12015-07-13 20:04:56 +000079#endif
Howard Hinnantd1d27a42011-06-03 19:40:40 +000080 }
Marshall Clow7d914d12015-07-13 20:04:56 +000081#if TEST_STD_VER >= 14
82 {
83 typedef std::vector<MoveOnly, some_alloc2<MoveOnly>> C;
84 C c1, c2;
85 // if the allocators are always equal, then the swap can be noexcept
86 static_assert( noexcept(swap(c1, c2)), "");
87 }
88#endif
Dan Albert1d4a1ed2016-05-25 22:36:09 -070089
90#endif
Howard Hinnantd1d27a42011-06-03 19:40:40 +000091}