1 //===----------------------------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // UNSUPPORTED: c++98, c++03, c++11, c++14
13 // optional(const optional<T>& rhs);
16 #include <type_traits>
19 #include "test_macros.h"
20 #include "archetypes.hpp"
24 template <class T, class ...InitArgs>
25 void test(InitArgs&&... args)
27 const optional<T> rhs(std::forward<InitArgs>(args)...);
28 bool rhs_engaged = static_cast<bool>(rhs);
29 optional<T> lhs = rhs;
30 assert(static_cast<bool>(lhs) == rhs_engaged);
35 void test_throwing_ctor() {
36 #ifndef TEST_HAS_NO_EXCEPTIONS
39 Z(Z const& o) : count(o.count + 1)
40 { if (count == 2) throw 6; }
44 const optional<Z> rhs(z);
57 template <class T, class ...InitArgs>
58 void test_ref(InitArgs&&... args)
60 const optional<T> rhs(std::forward<InitArgs>(args)...);
61 bool rhs_engaged = static_cast<bool>(rhs);
62 optional<T> lhs = rhs;
63 assert(static_cast<bool>(lhs) == rhs_engaged);
65 assert(&(*lhs) == &(*rhs));
69 void test_reference_extension()
71 #if defined(_LIBCPP_VERSION) && 0 // FIXME these extensions are currently disabled.
72 using T = TestTypes::TestType;
76 T::reset_constructors();
79 assert(T::alive == 1);
80 assert(T::constructed == 0);
81 assert(T::assigned == 0);
82 assert(T::destroyed == 0);
84 assert(T::destroyed == 1);
85 assert(T::alive == 0);
89 T::reset_constructors();
91 test_ref<T const&>(t);
92 test_ref<T const&>(ct);
93 assert(T::alive == 1);
94 assert(T::constructed == 0);
95 assert(T::assigned == 0);
96 assert(T::destroyed == 0);
98 assert(T::alive == 0);
99 assert(T::destroyed == 1);
101 static_assert(!std::is_copy_constructible<std::optional<T&&>>::value, "");
102 static_assert(!std::is_copy_constructible<std::optional<T const&&>>::value, "");
112 const optional<const int> o(42);
113 optional<const int> o2(o);
117 using T = TestTypes::TestType;
119 const optional<T> rhs;
120 assert(T::alive == 0);
121 const optional<T> lhs(rhs);
122 assert(lhs.has_value() == false);
123 assert(T::alive == 0);
125 TestTypes::TestType::reset();
127 using T = TestTypes::TestType;
129 const optional<T> rhs(42);
130 assert(T::alive == 1);
131 assert(T::value_constructed == 1);
132 assert(T::copy_constructed == 0);
133 const optional<T> lhs(rhs);
134 assert(lhs.has_value());
135 assert(T::copy_constructed == 1);
136 assert(T::alive == 2);
138 TestTypes::TestType::reset();
140 using namespace ConstexprTestTypes;
145 using namespace TrivialTestTypes;
150 test_throwing_ctor();
153 test_reference_extension();