move_assign_noexcept.pass.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. // basic_string& operator=(basic_string&& c)
  12. // noexcept(
  13. // allocator_traits<allocator_type>::propagate_on_container_move_assignment::value ||
  14. // allocator_traits<allocator_type>::is_always_equal::value); // C++17
  15. //
  16. // before C++17, we use the conforming extension
  17. // noexcept(
  18. // allocator_type::propagate_on_container_move_assignment::value &&
  19. // is_nothrow_move_assignable<allocator_type>::value);
  20. #include <string>
  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(const some_alloc&);
  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_move_assignment;
  38. typedef std::true_type is_always_equal;
  39. };
  40. template <class T>
  41. struct some_alloc3
  42. {
  43. typedef T value_type;
  44. some_alloc3() {}
  45. some_alloc3(const some_alloc3&);
  46. void deallocate(void*, unsigned) {}
  47. typedef std::false_type propagate_on_container_move_assignment;
  48. typedef std::false_type is_always_equal;
  49. };
  50. int main()
  51. {
  52. {
  53. typedef std::string C;
  54. static_assert(std::is_nothrow_move_assignable<C>::value, "");
  55. }
  56. {
  57. typedef std::basic_string<char, std::char_traits<char>, test_allocator<char>> C;
  58. static_assert(!std::is_nothrow_move_assignable<C>::value, "");
  59. }
  60. {
  61. typedef std::basic_string<char, std::char_traits<char>, some_alloc<char>> C;
  62. #if TEST_STD_VER > 14
  63. // if the allocators are always equal, then the move assignment can be noexcept
  64. static_assert( std::is_nothrow_move_assignable<C>::value, "");
  65. #else
  66. static_assert(!std::is_nothrow_move_assignable<C>::value, "");
  67. #endif
  68. }
  69. #if TEST_STD_VER > 14
  70. {
  71. // POCMA is false, always equal
  72. typedef std::basic_string<char, std::char_traits<char>, some_alloc2<char>> C;
  73. static_assert( std::is_nothrow_move_assignable<C>::value, "");
  74. }
  75. {
  76. // POCMA is false, not always equal
  77. typedef std::basic_string<char, std::char_traits<char>, some_alloc3<char>> C;
  78. static_assert(!std::is_nothrow_move_assignable<C>::value, "");
  79. }
  80. #endif
  81. }