swap_noexcept.pass.cpp 2.3 KB

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