swap_noexcept.pass.cpp revision 84acb1ec3f7d5e0f37d7176697c2fa876c413407
1//===----------------------------------------------------------------------===//
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// UNSUPPORTED: c++98, c++03
11
12// <list>
13
14// void swap(list& c)
15//     noexcept(!allocator_type::propagate_on_container_swap::value ||
16//              __is_nothrow_swappable<allocator_type>::value);
17//
18//  In C++17, the standard says that swap shall have:
19//     noexcept(allocator_traits<Allocator>::is_always_equal::value);
20
21// This tests a conforming extension
22
23#include <list>
24#include <cassert>
25
26#include "test_macros.h"
27#include "MoveOnly.h"
28#include "test_allocator.h"
29
30template <class T>
31struct some_alloc
32{
33    typedef T value_type;
34
35    some_alloc() {}
36    some_alloc(const some_alloc&);
37    void deallocate(void*, unsigned) {}
38
39    typedef std::true_type propagate_on_container_swap;
40};
41
42template <class T>
43struct some_alloc2
44{
45    typedef T value_type;
46
47    some_alloc2() {}
48    some_alloc2(const some_alloc2&);
49    void deallocate(void*, unsigned) {}
50
51    typedef std::false_type propagate_on_container_swap;
52    typedef std::true_type is_always_equal;
53};
54
55int main()
56{
57    {
58        typedef std::list<MoveOnly> C;
59        C c1, c2;
60        static_assert(noexcept(swap(c1, c2)), "");
61    }
62    {
63        typedef std::list<MoveOnly, test_allocator<MoveOnly>> C;
64        C c1, c2;
65        static_assert(noexcept(swap(c1, c2)), "");
66    }
67    {
68        typedef std::list<MoveOnly, other_allocator<MoveOnly>> C;
69        C c1, c2;
70        static_assert(noexcept(swap(c1, c2)), "");
71    }
72    {
73        typedef std::list<MoveOnly, some_alloc<MoveOnly>> C;
74        C c1, c2;
75#if TEST_STD_VER >= 14
76    //  In c++14, if POCS is set, swapping the allocator is required not to throw
77        static_assert( noexcept(swap(c1, c2)), "");
78#else
79        static_assert(!noexcept(swap(c1, c2)), "");
80#endif
81    }
82#if TEST_STD_VER >= 14
83    {
84        typedef std::list<MoveOnly, some_alloc2<MoveOnly>> C;
85        C c1, c2;
86    //  if the allocators are always equal, then the swap can be noexcept
87        static_assert( noexcept(swap(c1, c2)), "");
88    }
89#endif
90
91}
92