swap_noexcept.pass.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 "test_allocator.h"
  24. template <class T>
  25. struct some_alloc
  26. {
  27. typedef T value_type;
  28. some_alloc() {}
  29. some_alloc(const some_alloc&);
  30. void deallocate(void*, unsigned) {}
  31. typedef std::true_type propagate_on_container_swap;
  32. };
  33. template <class T>
  34. struct some_alloc2
  35. {
  36. typedef T value_type;
  37. some_alloc2() {}
  38. some_alloc2(const some_alloc2&);
  39. void deallocate(void*, unsigned) {}
  40. typedef std::false_type propagate_on_container_swap;
  41. typedef std::true_type is_always_equal;
  42. };
  43. int main()
  44. {
  45. #if defined(_LIBCPP_VERSION)
  46. {
  47. typedef std::vector<bool> C;
  48. static_assert(noexcept(swap(std::declval<C&>(), std::declval<C&>())), "");
  49. }
  50. {
  51. typedef std::vector<bool, test_allocator<bool>> C;
  52. static_assert(noexcept(swap(std::declval<C&>(), std::declval<C&>())), "");
  53. }
  54. {
  55. typedef std::vector<bool, other_allocator<bool>> C;
  56. static_assert(noexcept(swap(std::declval<C&>(), std::declval<C&>())), "");
  57. }
  58. #endif // _LIBCPP_VERSION
  59. {
  60. #if TEST_STD_VER >= 14
  61. #if defined(_LIBCPP_VERSION)
  62. // In C++14, if POCS is set, swapping the allocator is required not to throw
  63. typedef std::vector<bool, some_alloc<bool>> C;
  64. static_assert( noexcept(swap(std::declval<C&>(), std::declval<C&>())), "");
  65. #endif // _LIBCPP_VERSION
  66. #else
  67. typedef std::vector<bool, some_alloc<bool>> C;
  68. static_assert(!noexcept(swap(std::declval<C&>(), std::declval<C&>())), "");
  69. #endif
  70. }
  71. #if TEST_STD_VER >= 14
  72. #if defined(_LIBCPP_VERSION)
  73. {
  74. typedef std::vector<bool, some_alloc2<bool>> C;
  75. // if the allocators are always equal, then the swap can be noexcept
  76. static_assert( noexcept(swap(std::declval<C&>(), std::declval<C&>())), "");
  77. }
  78. #endif // _LIBCPP_VERSION
  79. #endif
  80. }