blob: cbe8142eeb4b094ab0ac9b1416de4102163e85f1 [file] [log] [blame]
Howard Hinnantb965fed2011-06-03 16:20:53 +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// <forward_list>
11
12// void swap(forward_list& 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(is_always_equal<allocator_type>::value);
Howard Hinnantb965fed2011-06-03 16:20:53 +000018
19// This tests a conforming extension
20
21#include <forward_list>
22#include <cassert>
23
Marshall Clowdf00d5e2015-01-28 21:22:53 +000024#include "MoveOnly.h"
Marshall Clow1b921882013-12-03 00:18:10 +000025#include "test_allocator.h"
Howard Hinnantb965fed2011-06-03 16:20:53 +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 Hinnantb965fed2011-06-03 16:20:53 +000052int main()
53{
Dan Albert1d4a1ed2016-05-25 22:36:09 -070054#if __has_feature(cxx_noexcept)
Howard Hinnantb965fed2011-06-03 16:20:53 +000055 {
56 typedef std::forward_list<MoveOnly> C;
57 C c1, c2;
58 static_assert(noexcept(swap(c1, c2)), "");
59 }
60 {
61 typedef std::forward_list<MoveOnly, test_allocator<MoveOnly>> C;
62 C c1, c2;
63 static_assert(noexcept(swap(c1, c2)), "");
64 }
65 {
66 typedef std::forward_list<MoveOnly, other_allocator<MoveOnly>> C;
67 C c1, c2;
68 static_assert(noexcept(swap(c1, c2)), "");
69 }
70 {
71 typedef std::forward_list<MoveOnly, some_alloc<MoveOnly>> 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 Hinnantb965fed2011-06-03 16:20:53 +000077 static_assert(!noexcept(swap(c1, c2)), "");
Marshall Clow7d914d12015-07-13 20:04:56 +000078#endif
Howard Hinnantb965fed2011-06-03 16:20:53 +000079 }
Marshall Clow7d914d12015-07-13 20:04:56 +000080#if TEST_STD_VER >= 14
81 {
82 typedef std::forward_list<MoveOnly, some_alloc2<MoveOnly>> 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 Hinnantb965fed2011-06-03 16:20:53 +000090}