swap_noexcept.pass.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. // <string>
  11. // void swap(basic_string& 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 <string>
  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. {
  46. typedef std::string C;
  47. static_assert(noexcept(swap(std::declval<C&>(), std::declval<C&>())), "");
  48. }
  49. #if defined(_LIBCPP_VERSION)
  50. {
  51. typedef std::basic_string<char, std::char_traits<char>, test_allocator<char>> C;
  52. static_assert(noexcept(swap(std::declval<C&>(), std::declval<C&>())), "");
  53. }
  54. #endif // _LIBCPP_VERSION
  55. {
  56. typedef std::basic_string<char, std::char_traits<char>, some_alloc<char>> C;
  57. #if TEST_STD_VER >= 14
  58. // In C++14, if POCS is set, swapping the allocator is required not to throw
  59. static_assert( noexcept(swap(std::declval<C&>(), std::declval<C&>())), "");
  60. #else
  61. static_assert(!noexcept(swap(std::declval<C&>(), std::declval<C&>())), "");
  62. #endif
  63. }
  64. #if TEST_STD_VER >= 14
  65. {
  66. typedef std::basic_string<char, std::char_traits<char>, some_alloc2<char>> C;
  67. // if the allocators are always equal, then the swap can be noexcept
  68. static_assert( noexcept(swap(std::declval<C&>(), std::declval<C&>())), "");
  69. }
  70. #endif
  71. }