assign_value.pass.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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, c++11
  10. // <optional>
  11. // template <class U> optional<T>& operator=(U&& v);
  12. #include <experimental/optional>
  13. #include <type_traits>
  14. #include <cassert>
  15. #include <memory>
  16. using std::experimental::optional;
  17. struct AllowConstAssign {
  18. AllowConstAssign() {}
  19. AllowConstAssign(AllowConstAssign const&) {}
  20. AllowConstAssign const& operator=(AllowConstAssign const&) const {
  21. return *this;
  22. }
  23. };
  24. struct X
  25. {
  26. };
  27. int main()
  28. {
  29. static_assert(std::is_assignable<optional<int>, int>::value, "");
  30. static_assert(std::is_assignable<optional<int>, int&>::value, "");
  31. static_assert(std::is_assignable<optional<int>&, int>::value, "");
  32. static_assert(std::is_assignable<optional<int>&, int&>::value, "");
  33. static_assert(std::is_assignable<optional<int>&, const int&>::value, "");
  34. static_assert(!std::is_assignable<const optional<int>&, const int&>::value, "");
  35. static_assert(!std::is_assignable<optional<int>, X>::value, "");
  36. {
  37. optional<int> opt;
  38. opt = 1;
  39. assert(static_cast<bool>(opt) == true);
  40. assert(*opt == 1);
  41. }
  42. {
  43. optional<int> opt;
  44. const int i = 2;
  45. opt = i;
  46. assert(static_cast<bool>(opt) == true);
  47. assert(*opt == i);
  48. }
  49. {
  50. optional<int> opt(3);
  51. const int i = 2;
  52. opt = i;
  53. assert(static_cast<bool>(opt) == true);
  54. assert(*opt == i);
  55. }
  56. {
  57. optional<const AllowConstAssign> opt;
  58. const AllowConstAssign other;
  59. opt = other;
  60. }
  61. {
  62. optional<std::unique_ptr<int>> opt;
  63. opt = std::unique_ptr<int>(new int(3));
  64. assert(static_cast<bool>(opt) == true);
  65. assert(**opt == 3);
  66. }
  67. {
  68. optional<std::unique_ptr<int>> opt(std::unique_ptr<int>(new int(2)));
  69. opt = std::unique_ptr<int>(new int(3));
  70. assert(static_cast<bool>(opt) == true);
  71. assert(**opt == 3);
  72. }
  73. }