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