nullopt_t.pass.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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, c++14
  10. // <optional>
  11. // constexpr optional(nullopt_t) noexcept;
  12. #include <optional>
  13. #include <type_traits>
  14. #include <cassert>
  15. #include "archetypes.hpp"
  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()
  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. }