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