swap_noexcept.pass.cpp 2.5 KB

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