U.pass.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. //
  10. // UNSUPPORTED: c++98, c++03, c++11, c++14
  11. // <optional>
  12. // template <class U>
  13. // constexpr EXPLICIT optional(U&& u);
  14. #include <optional>
  15. #include <type_traits>
  16. #include <cassert>
  17. #include "test_macros.h"
  18. #include "archetypes.hpp"
  19. #include "test_convertible.hpp"
  20. using std::optional;
  21. struct ImplicitThrow
  22. {
  23. constexpr ImplicitThrow(int x) { if (x != -1) TEST_THROW(6);}
  24. };
  25. struct ExplicitThrow
  26. {
  27. constexpr explicit ExplicitThrow(int x) { if (x != -1) TEST_THROW(6);}
  28. };
  29. template <class To, class From>
  30. constexpr bool implicit_conversion(optional<To>&& opt, const From& v)
  31. {
  32. using O = optional<To>;
  33. static_assert(test_convertible<O, From>(), "");
  34. static_assert(!test_convertible<O, void*>(), "");
  35. static_assert(!test_convertible<O, From, int>(), "");
  36. return opt && *opt == static_cast<To>(v);
  37. }
  38. template <class To, class Input, class Expect>
  39. constexpr bool explicit_conversion(Input&& in, const Expect& v)
  40. {
  41. using O = optional<To>;
  42. static_assert(std::is_constructible<O, Input>::value, "");
  43. static_assert(!std::is_convertible<Input, O>::value, "");
  44. static_assert(!std::is_constructible<O, void*>::value, "");
  45. static_assert(!std::is_constructible<O, Input, int>::value, "");
  46. optional<To> opt(std::forward<Input>(in));
  47. return opt && *opt == static_cast<To>(v);
  48. }
  49. void test_implicit()
  50. {
  51. {
  52. using T = long long;
  53. static_assert(implicit_conversion<long long>(42, 42), "");
  54. }
  55. {
  56. using T = long double;
  57. static_assert(implicit_conversion<long double>(3.14, 3.14), "");
  58. }
  59. {
  60. using T = TrivialTestTypes::TestType;
  61. static_assert(implicit_conversion<T>(42, 42), "");
  62. }
  63. {
  64. using T = TestTypes::TestType;
  65. assert(implicit_conversion<T>(3, T(3)));
  66. }
  67. #ifndef TEST_HAS_NO_EXCEPTIONS
  68. {
  69. try {
  70. using T = ImplicitThrow;
  71. optional<T> t = 42;
  72. assert(false);
  73. } catch (int) {
  74. }
  75. }
  76. #endif
  77. }
  78. void test_explicit() {
  79. {
  80. using T = ExplicitTrivialTestTypes::TestType;
  81. using O = optional<T>;
  82. static_assert(explicit_conversion<T>(42, 42), "");
  83. }
  84. {
  85. using T = ExplicitConstexprTestTypes::TestType;
  86. using O = optional<T>;
  87. static_assert(explicit_conversion<T>(42, 42), "");
  88. static_assert(!std::is_convertible<int, T>::value, "");
  89. }
  90. {
  91. using T = ExplicitTestTypes::TestType;
  92. using O = optional<T>;
  93. T::reset();
  94. {
  95. assert(explicit_conversion<T>(42, 42));
  96. assert(T::alive == 0);
  97. }
  98. T::reset();
  99. {
  100. optional<T> t(42);
  101. assert(T::alive == 1);
  102. assert(T::value_constructed == 1);
  103. assert(T::move_constructed == 0);
  104. assert(T::copy_constructed == 0);
  105. assert(t.value().value == 42);
  106. }
  107. assert(T::alive == 0);
  108. }
  109. #ifndef TEST_HAS_NO_EXCEPTIONS
  110. {
  111. try {
  112. using T = ExplicitThrow;
  113. optional<T> t(42);
  114. assert(false);
  115. } catch (int) {
  116. }
  117. }
  118. #endif
  119. }
  120. int main() {
  121. test_implicit();
  122. test_explicit();
  123. }