default.pass.cpp 2.2 KB

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