nullopt_t.pass.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. // UNSUPPORTED: c++98, c++03, c++11, c++14
  9. // <optional>
  10. // constexpr optional(nullopt_t) noexcept;
  11. #include <optional>
  12. #include <type_traits>
  13. #include <cassert>
  14. #include "archetypes.h"
  15. #include "test_macros.h"
  16. using std::optional;
  17. using std::nullopt_t;
  18. using std::nullopt;
  19. template <class Opt>
  20. void
  21. test_constexpr()
  22. {
  23. static_assert(std::is_nothrow_constructible<Opt, nullopt_t&>::value, "");
  24. static_assert(std::is_trivially_destructible<Opt>::value, "");
  25. static_assert(std::is_trivially_destructible<typename Opt::value_type>::value, "");
  26. constexpr Opt opt(nullopt);
  27. static_assert(static_cast<bool>(opt) == false, "");
  28. struct test_constexpr_ctor
  29. : public Opt
  30. {
  31. constexpr test_constexpr_ctor() {}
  32. };
  33. }
  34. template <class Opt>
  35. void
  36. test()
  37. {
  38. static_assert(std::is_nothrow_constructible<Opt, nullopt_t&>::value, "");
  39. static_assert(!std::is_trivially_destructible<Opt>::value, "");
  40. static_assert(!std::is_trivially_destructible<typename Opt::value_type>::value, "");
  41. {
  42. Opt opt(nullopt);
  43. assert(static_cast<bool>(opt) == false);
  44. }
  45. {
  46. const Opt opt(nullopt);
  47. assert(static_cast<bool>(opt) == false);
  48. }
  49. struct test_constexpr_ctor
  50. : public Opt
  51. {
  52. constexpr test_constexpr_ctor() {}
  53. };
  54. }
  55. int main(int, char**)
  56. {
  57. test_constexpr<optional<int>>();
  58. test_constexpr<optional<int*>>();
  59. test_constexpr<optional<ImplicitTypes::NoCtors>>();
  60. test_constexpr<optional<NonTrivialTypes::NoCtors>>();
  61. test_constexpr<optional<NonConstexprTypes::NoCtors>>();
  62. test<optional<NonLiteralTypes::NoCtors>>();
  63. return 0;
  64. }