swap_noexcept.pass.cpp 2.4 KB

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