default.pass.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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() noexcept;
  11. #include <optional>
  12. #include <type_traits>
  13. #include <cassert>
  14. #include "test_macros.h"
  15. #include "archetypes.h"
  16. using std::optional;
  17. template <class Opt>
  18. void
  19. test_constexpr()
  20. {
  21. static_assert(std::is_nothrow_default_constructible<Opt>::value, "");
  22. static_assert(std::is_trivially_destructible<Opt>::value, "");
  23. static_assert(std::is_trivially_destructible<typename Opt::value_type>::value, "");
  24. constexpr Opt opt;
  25. static_assert(static_cast<bool>(opt) == false, "");
  26. struct test_constexpr_ctor
  27. : public Opt
  28. {
  29. constexpr test_constexpr_ctor() {}
  30. };
  31. }
  32. template <class Opt>
  33. void
  34. test()
  35. {
  36. static_assert(std::is_nothrow_default_constructible<Opt>::value, "");
  37. static_assert(!std::is_trivially_destructible<Opt>::value, "");
  38. static_assert(!std::is_trivially_destructible<typename Opt::value_type>::value, "");
  39. {
  40. Opt opt;
  41. assert(static_cast<bool>(opt) == false);
  42. }
  43. {
  44. const Opt opt;
  45. assert(static_cast<bool>(opt) == false);
  46. }
  47. struct test_constexpr_ctor
  48. : public Opt
  49. {
  50. constexpr test_constexpr_ctor() {}
  51. };
  52. }
  53. int main(int, char**)
  54. {
  55. test_constexpr<optional<int>>();
  56. test_constexpr<optional<int*>>();
  57. test_constexpr<optional<ImplicitTypes::NoCtors>>();
  58. test_constexpr<optional<NonTrivialTypes::NoCtors>>();
  59. test_constexpr<optional<NonConstexprTypes::NoCtors>>();
  60. test<optional<NonLiteralTypes::NoCtors>>();
  61. // EXTENSIONS
  62. #if defined(_LIBCPP_VERSION) && 0 // FIXME these extensions are currently disabled.
  63. test_constexpr<optional<int&>>();
  64. test_constexpr<optional<const int&>>();
  65. test_constexpr<optional<int&>>();
  66. test_constexpr<optional<NonLiteralTypes::NoCtors&>>();
  67. test_constexpr<optional<NonLiteralTypes::NoCtors&&>>();
  68. #endif
  69. return 0;
  70. }