libcxx

libcxx mirror with random patches
git clone https://git.neptards.moe/neptards/libcxx.git
Log | Files | Refs

swap_noexcept.pass.cpp (2590B)


      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 <utility>
     26 #include <cassert>
     27 
     28 #include "test_macros.h"
     29 #include "MoveOnly.h"
     30 #include "test_allocator.h"
     31 
     32 template <class T>
     33 struct some_alloc
     34 {
     35     typedef T value_type;
     36 
     37     some_alloc() {}
     38     some_alloc(const some_alloc&);
     39     void deallocate(void*, unsigned) {}
     40 
     41     typedef std::true_type propagate_on_container_swap;
     42 };
     43 
     44 template <class T>
     45 struct some_alloc2
     46 {
     47     typedef T value_type;
     48 
     49     some_alloc2() {}
     50     some_alloc2(const some_alloc2&);
     51     void deallocate(void*, unsigned) {}
     52 
     53     typedef std::false_type propagate_on_container_swap;
     54     typedef std::true_type is_always_equal;
     55 };
     56 
     57 int main()
     58 {
     59     {
     60         typedef std::vector<MoveOnly> C;
     61         static_assert(noexcept(swap(std::declval<C&>(), std::declval<C&>())), "");
     62     }
     63 #if defined(_LIBCPP_VERSION)
     64     {
     65         typedef std::vector<MoveOnly, test_allocator<MoveOnly>> C;
     66         static_assert(noexcept(swap(std::declval<C&>(), std::declval<C&>())), "");
     67     }
     68 #endif // _LIBCPP_VERSION
     69     {
     70         typedef std::vector<MoveOnly, other_allocator<MoveOnly>> C;
     71         static_assert(noexcept(swap(std::declval<C&>(), std::declval<C&>())), "");
     72     }
     73     {
     74         typedef std::vector<MoveOnly, some_alloc<MoveOnly>> C;
     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(std::declval<C&>(), std::declval<C&>())), "");
     78 #else
     79         static_assert(!noexcept(swap(std::declval<C&>(), std::declval<C&>())), "");
     80 #endif
     81     }
     82 #if TEST_STD_VER >= 14
     83     {
     84         typedef std::vector<MoveOnly, some_alloc2<MoveOnly>> C;
     85     //  if the allocators are always equal, then the swap can be noexcept
     86         static_assert( noexcept(swap(std::declval<C&>(), std::declval<C&>())), "");
     87     }
     88 #endif
     89 }