Move <optional> into include/experimental, and into the std::experimental namespace, since it's not part of C++14, but of an upcoming TS

llvm-svn: 194867
This commit is contained in:
Marshall Clow 2013-11-15 22:42:10 +00:00
parent 3f823e3af1
commit dfdac03c8f
59 changed files with 523 additions and 409 deletions

View File

@ -18,8 +18,7 @@
#include <initializer_list>
namespace std
{
namespace std { namespace experimental {
// optional for object types
template <class T>
@ -110,7 +109,7 @@ template <class T> constexpr optional<typename decay<T>::type> make_optional(T&&
template <class T> struct hash;
template <class T> struct hash<optional<T>>;
} // std
}} // std::experimental
*/
@ -118,8 +117,7 @@ template <class T> struct hash<optional<T>>;
#include <functional>
#include <stdexcept>
namespace std // purposefully not using versioning namespace
{
namespace std { namespace experimental {
class _LIBCPP_EXCEPTION_ABI bad_optional_access
: public logic_error
@ -142,7 +140,7 @@ public:
virtual ~bad_optional_access() _NOEXCEPT;
};
} // std
}} // std::experimental
#if _LIBCPP_STD_VER > 11
@ -163,7 +161,7 @@ public:
#pragma GCC system_header
#endif
_LIBCPP_BEGIN_NAMESPACE_STD
namespace std { namespace experimental { inline namespace __library_fundamentals_v1 {
struct in_place_t {};
constexpr in_place_t in_place{};
@ -677,10 +675,14 @@ make_optional(_Tp&& __v)
return optional<typename decay<_Tp>::type>(_VSTD::forward<_Tp>(__v));
}
}}} // namespace std::experimental::__library_fundamentals_v1
_LIBCPP_BEGIN_NAMESPACE_STD
template <class _Tp>
struct _LIBCPP_TYPE_VIS_ONLY hash<optional<_Tp> >
struct _LIBCPP_TYPE_VIS_ONLY hash<std::experimental::optional<_Tp> >
{
typedef optional<_Tp> argument_type;
typedef std::experimental::optional<_Tp> argument_type;
typedef size_t result_type;
_LIBCPP_INLINE_VISIBILITY

View File

@ -7,10 +7,10 @@
//
//===----------------------------------------------------------------------===//
#include "optional"
#include "experimental/optional"
namespace std // purposefully not using versioning namespace
{
{ namespace experimental {
#ifdef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
@ -22,4 +22,4 @@ bad_optional_access::~bad_optional_access() _NOEXCEPT = default;
#endif
} // std
}} // std::experimental

View File

@ -12,20 +12,18 @@
// class bad_optional_access;
// explicit bad_optional_access(const char* what_arg);
#include <optional>
#include <experimental/optional>
#include <string>
#include <cstring>
#include <cassert>
#if _LIBCPP_STD_VER > 11
#endif // _LIBCPP_STD_VER > 11
int main()
{
#if _LIBCPP_STD_VER > 11
using std::experimental::bad_optional_access;
const char* s = "message";
std::bad_optional_access e(s);
bad_optional_access e(s);
assert(std::strcmp(e.what(), s) == 0);
#endif // _LIBCPP_STD_VER > 11
}

View File

@ -12,24 +12,22 @@
// class bad_optional_access;
// bad_optional_access& operator=(const bad_optional_access&);
#include <optional>
#include <experimental/optional>
#include <string>
#include <cstring>
#include <type_traits>
#include <cassert>
#if _LIBCPP_STD_VER > 11
#endif // _LIBCPP_STD_VER > 11
int main()
{
#if _LIBCPP_STD_VER > 11
static_assert(std::is_nothrow_copy_assignable<std::bad_optional_access>::value, "");
using std::experimental::bad_optional_access;
static_assert(std::is_nothrow_copy_assignable<bad_optional_access>::value, "");
const std::string s1("one message");
const std::string s2("another message");
std::bad_optional_access e1(s1);
std::bad_optional_access e2(s2);
bad_optional_access e1(s1);
bad_optional_access e2(s2);
assert(std::strcmp(e1.what(), e2.what()) != 0);
e1 = e2;
assert(std::strcmp(e1.what(), e2.what()) == 0);

View File

@ -12,23 +12,21 @@
// class bad_optional_access;
// bad_optional_access(const bad_optional_access&);
#include <optional>
#include <experimental/optional>
#include <string>
#include <cstring>
#include <type_traits>
#include <cassert>
#if _LIBCPP_STD_VER > 11
#endif // _LIBCPP_STD_VER > 11
int main()
{
#if _LIBCPP_STD_VER > 11
static_assert(std::is_nothrow_copy_constructible<std::bad_optional_access>::value, "");
using std::experimental::bad_optional_access;
static_assert(std::is_nothrow_copy_constructible<bad_optional_access>::value, "");
const std::string s("another message");
std::bad_optional_access e1(s);
std::bad_optional_access e2 = e1;
bad_optional_access e1(s);
bad_optional_access e2 = e1;
assert(std::strcmp(e1.what(), e2.what()) == 0);
#endif // _LIBCPP_STD_VER > 11
}

View File

@ -11,16 +11,14 @@
// class bad_optional_access is not default constructible
#include <optional>
#include <experimental/optional>
#include <type_traits>
#if _LIBCPP_STD_VER > 11
#endif // _LIBCPP_STD_VER > 11
int main()
{
#if _LIBCPP_STD_VER > 11
static_assert(!std::is_default_constructible<std::bad_optional_access>::value, "");
using std::experimental::bad_optional_access;
static_assert(!std::is_default_constructible<bad_optional_access>::value, "");
#endif // _LIBCPP_STD_VER > 11
}

View File

@ -11,17 +11,15 @@
// class bad_optional_access : public logic_error
#include <optional>
#include <experimental/optional>
#include <type_traits>
#if _LIBCPP_STD_VER > 11
#endif // _LIBCPP_STD_VER > 11
int main()
{
#if _LIBCPP_STD_VER > 11
static_assert(std::is_base_of<std::logic_error, std::bad_optional_access>::value, "");
static_assert(std::is_convertible<std::bad_optional_access*, std::logic_error*>::value, "");
using std::experimental::bad_optional_access;
static_assert(std::is_base_of<std::logic_error, bad_optional_access>::value, "");
static_assert(std::is_convertible<bad_optional_access*, std::logic_error*>::value, "");
#endif // _LIBCPP_STD_VER > 11
}

View File

@ -12,20 +12,18 @@
// class bad_optional_access;
// explicit bad_optional_access(const string& what_arg);
#include <optional>
#include <experimental/optional>
#include <string>
#include <cstring>
#include <cassert>
#if _LIBCPP_STD_VER > 11
#endif // _LIBCPP_STD_VER > 11
int main()
{
#if _LIBCPP_STD_VER > 11
using std::experimental::bad_optional_access;
const std::string s("message");
std::bad_optional_access e(s);
bad_optional_access e(s);
assert(std::strcmp(e.what(), s.c_str()) == 0);
#endif // _LIBCPP_STD_VER > 11
}

View File

@ -12,10 +12,12 @@
// template <class T> constexpr bool operator==(const optional<T>& x, const T& v);
// template <class T> constexpr bool operator==(const T& v, const optional<T>& x);
#include <optional>
#include <experimental/optional>
#if _LIBCPP_STD_VER > 11
using std::experimental::optional;
struct X
{
int i_;
@ -33,7 +35,7 @@ int main()
#if _LIBCPP_STD_VER > 11
{
typedef X T;
typedef std::optional<T> O;
typedef optional<T> O;
constexpr T val(2);
constexpr O o1; // disengaged

View File

@ -12,10 +12,12 @@
// template <class T> constexpr bool operator<(const optional<T>& x, const T& v);
// template <class T> constexpr bool operator<(const T& v, const optional<T>& x);
#include <optional>
#include <experimental/optional>
#if _LIBCPP_STD_VER > 11
using std::experimental::optional;
struct X
{
int i_;
@ -34,7 +36,7 @@ int main()
{
typedef X T;
typedef std::optional<T> O;
typedef optional<T> O;
constexpr T val(2);
constexpr O o1; // disengaged

View File

@ -11,38 +11,37 @@
// template <class T> struct hash<optional<T>>;
#include <optional>
#include <experimental/optional>
#include <string>
#include <memory>
#include <cassert>
#if _LIBCPP_STD_VER > 11
#endif // _LIBCPP_STD_VER > 11
int main()
{
#if _LIBCPP_STD_VER > 11
using std::experimental::optional;
{
typedef int T;
std::optional<T> opt;
assert(std::hash<std::optional<T>>{}(opt) == 0);
optional<T> opt;
assert(std::hash<optional<T>>{}(opt) == 0);
opt = 2;
assert(std::hash<std::optional<T>>{}(opt) == std::hash<T>{}(*opt));
assert(std::hash<optional<T>>{}(opt) == std::hash<T>{}(*opt));
}
{
typedef std::string T;
std::optional<T> opt;
assert(std::hash<std::optional<T>>{}(opt) == 0);
optional<T> opt;
assert(std::hash<optional<T>>{}(opt) == 0);
opt = std::string("123");
assert(std::hash<std::optional<T>>{}(opt) == std::hash<T>{}(*opt));
assert(std::hash<optional<T>>{}(opt) == std::hash<T>{}(*opt));
}
{
typedef std::unique_ptr<int> T;
std::optional<T> opt;
assert(std::hash<std::optional<T>>{}(opt) == 0);
optional<T> opt;
assert(std::hash<optional<T>>{}(opt) == 0);
opt = std::unique_ptr<int>(new int(3));
assert(std::hash<std::optional<T>>{}(opt) == std::hash<T>{}(*opt));
assert(std::hash<optional<T>>{}(opt) == std::hash<T>{}(*opt));
}
#endif // _LIBCPP_STD_VER > 11
}

View File

@ -12,14 +12,18 @@
// struct in_place_t{};
// constexpr in_place_t in_place{};
#include <optional>
#include <experimental/optional>
#include <type_traits>
#if _LIBCPP_STD_VER > 11
using std::experimental::optional;
using std::experimental::in_place_t;
using std::experimental::in_place;
constexpr
int
test(const std::in_place_t&)
test(const in_place_t&)
{
return 3;
}
@ -30,9 +34,9 @@ int main()
{
#if _LIBCPP_STD_VER > 11
static_assert((std::is_class<std::in_place_t>::value), "");
static_assert((std::is_empty<std::in_place_t>::value), "");
static_assert((std::is_class<in_place_t>::value), "");
static_assert((std::is_empty<in_place_t>::value), "");
static_assert(test(std::in_place) == 3, "");
static_assert(test(in_place) == 3, "");
#endif
}

View File

@ -13,25 +13,29 @@
// template <class T> constexpr bool operator==(const optional<T>& x, nullopt_t) noexcept;
// template <class T> constexpr bool operator==(nullopt_t, const optional<T>& x) noexcept;
#include <optional>
#include <experimental/optional>
int main()
{
#if _LIBCPP_STD_VER > 11
using std::experimental::optional;
using std::experimental::nullopt_t;
using std::experimental::nullopt;
{
typedef int T;
typedef std::optional<T> O;
typedef optional<T> O;
constexpr O o1; // disengaged
constexpr O o2{1}; // engaged
static_assert ( std::nullopt == o1 , "" );
static_assert ( !(std::nullopt == o2), "" );
static_assert ( o1 == std::nullopt , "" );
static_assert ( !(o2 == std::nullopt), "" );
static_assert ( nullopt == o1 , "" );
static_assert ( !(nullopt == o2), "" );
static_assert ( o1 == nullopt , "" );
static_assert ( !(o2 == nullopt), "" );
static_assert (noexcept(std::nullopt == o1), "");
static_assert (noexcept(o1 == std::nullopt), "");
static_assert (noexcept(nullopt == o1), "");
static_assert (noexcept(o1 == nullopt), "");
}
#endif
}

View File

@ -13,25 +13,29 @@
// template <class T> constexpr bool operator<(const optional<T>& x, nullopt_t) noexcept;
// template <class T> constexpr bool operator<(nullopt_t, const optional<T>& x) noexcept;
#include <optional>
#include <experimental/optional>
int main()
{
#if _LIBCPP_STD_VER > 11
using std::experimental::optional;
using std::experimental::nullopt_t;
using std::experimental::nullopt;
{
typedef int T;
typedef std::optional<T> O;
typedef optional<T> O;
constexpr O o1; // disengaged
constexpr O o2{1}; // engaged
static_assert ( !(std::nullopt < o1), "" );
static_assert ( std::nullopt < o2 , "" );
static_assert ( !(o1 < std::nullopt), "" );
static_assert ( !(o2 < std::nullopt), "" );
static_assert ( !(nullopt < o1), "" );
static_assert ( nullopt < o2 , "" );
static_assert ( !(o1 < nullopt), "" );
static_assert ( !(o2 < nullopt), "" );
static_assert (noexcept(std::nullopt < o1), "");
static_assert (noexcept(o1 < std::nullopt), "");
static_assert (noexcept(nullopt < o1), "");
static_assert (noexcept(o1 < nullopt), "");
}
#endif
}

View File

@ -12,14 +12,18 @@
// struct nullopt_t{see below};
// constexpr nullopt_t nullopt(unspecified);
#include <optional>
#include <experimental/optional>
#include <type_traits>
#if _LIBCPP_STD_VER > 11
using std::experimental::optional;
using std::experimental::nullopt_t;
using std::experimental::nullopt;
constexpr
int
test(const std::nullopt_t&)
test(const nullopt_t&)
{
return 3;
}
@ -29,11 +33,11 @@ test(const std::nullopt_t&)
int main()
{
#if _LIBCPP_STD_VER > 11
static_assert((std::is_class<std::nullopt_t>::value), "");
static_assert((std::is_empty<std::nullopt_t>::value), "");
static_assert((std::is_literal_type<std::nullopt_t>::value), "");
static_assert((!std::is_default_constructible<std::nullopt_t>::value), "");
static_assert((std::is_class<nullopt_t>::value), "");
static_assert((std::is_empty<nullopt_t>::value), "");
static_assert((std::is_literal_type<nullopt_t>::value), "");
static_assert((!std::is_default_constructible<nullopt_t>::value), "");
static_assert(test(std::nullopt) == 3, "");
static_assert(test(nullopt) == 3, "");
#endif
}

View File

@ -11,13 +11,15 @@
// template <class U> optional<T>& operator=(U&& v);
#include <optional>
#include <experimental/optional>
#include <type_traits>
#include <cassert>
#include <memory>
#if _LIBCPP_STD_VER > 11
using std::experimental::optional;
struct X
{
};
@ -27,41 +29,41 @@ struct X
int main()
{
#if _LIBCPP_STD_VER > 11
static_assert(std::is_assignable<std::optional<int>, int>::value, "");
static_assert(std::is_assignable<std::optional<int>, int&>::value, "");
static_assert(std::is_assignable<std::optional<int>&, int>::value, "");
static_assert(std::is_assignable<std::optional<int>&, int&>::value, "");
static_assert(std::is_assignable<std::optional<int>&, const int&>::value, "");
static_assert(!std::is_assignable<const std::optional<int>&, const int&>::value, "");
static_assert(!std::is_assignable<std::optional<int>, X>::value, "");
static_assert(std::is_assignable<optional<int>, int>::value, "");
static_assert(std::is_assignable<optional<int>, int&>::value, "");
static_assert(std::is_assignable<optional<int>&, int>::value, "");
static_assert(std::is_assignable<optional<int>&, int&>::value, "");
static_assert(std::is_assignable<optional<int>&, const int&>::value, "");
static_assert(!std::is_assignable<const optional<int>&, const int&>::value, "");
static_assert(!std::is_assignable<optional<int>, X>::value, "");
{
std::optional<int> opt;
optional<int> opt;
opt = 1;
assert(static_cast<bool>(opt) == true);
assert(*opt == 1);
}
{
std::optional<int> opt;
optional<int> opt;
const int i = 2;
opt = i;
assert(static_cast<bool>(opt) == true);
assert(*opt == i);
}
{
std::optional<int> opt(3);
optional<int> opt(3);
const int i = 2;
opt = i;
assert(static_cast<bool>(opt) == true);
assert(*opt == i);
}
{
std::optional<std::unique_ptr<int>> opt;
optional<std::unique_ptr<int>> opt;
opt = std::unique_ptr<int>(new int(3));
assert(static_cast<bool>(opt) == true);
assert(**opt == 3);
}
{
std::optional<std::unique_ptr<int>> opt(std::unique_ptr<int>(new int(2)));
optional<std::unique_ptr<int>> opt(std::unique_ptr<int>(new int(2)));
opt = std::unique_ptr<int>(new int(3));
assert(static_cast<bool>(opt) == true);
assert(**opt == 3);

View File

@ -11,12 +11,14 @@
// optional<T>& operator=(const optional<T>& rhs);
#include <optional>
#include <experimental/optional>
#include <type_traits>
#include <cassert>
#if _LIBCPP_STD_VER > 11
using std::experimental::optional;
struct X
{
static bool throw_now;
@ -37,15 +39,15 @@ int main()
{
#if _LIBCPP_STD_VER > 11
{
std::optional<int> opt;
constexpr std::optional<int> opt2;
optional<int> opt;
constexpr optional<int> opt2;
opt = opt2;
static_assert(static_cast<bool>(opt2) == false, "");
assert(static_cast<bool>(opt) == static_cast<bool>(opt2));
}
{
std::optional<int> opt;
constexpr std::optional<int> opt2(2);
optional<int> opt;
constexpr optional<int> opt2(2);
opt = opt2;
static_assert(static_cast<bool>(opt2) == true, "");
static_assert(*opt2 == 2, "");
@ -53,15 +55,15 @@ int main()
assert(*opt == *opt2);
}
{
std::optional<int> opt(3);
constexpr std::optional<int> opt2;
optional<int> opt(3);
constexpr optional<int> opt2;
opt = opt2;
static_assert(static_cast<bool>(opt2) == false, "");
assert(static_cast<bool>(opt) == static_cast<bool>(opt2));
}
{
std::optional<int> opt(3);
constexpr std::optional<int> opt2(2);
optional<int> opt(3);
constexpr optional<int> opt2(2);
opt = opt2;
static_assert(static_cast<bool>(opt2) == true, "");
static_assert(*opt2 == 2, "");
@ -69,8 +71,8 @@ int main()
assert(*opt == *opt2);
}
{
std::optional<X> opt;
std::optional<X> opt2(X{});
optional<X> opt;
optional<X> opt2(X{});
assert(static_cast<bool>(opt2) == true);
try
{

View File

@ -11,13 +11,15 @@
// template <class... Args> void optional<T>::emplace(Args&&... args);
#include <optional>
#include <experimental/optional>
#include <type_traits>
#include <cassert>
#include <memory>
#if _LIBCPP_STD_VER > 11
using std::experimental::optional;
class X
{
int i_;
@ -58,61 +60,61 @@ int main()
{
#if _LIBCPP_STD_VER > 11
{
std::optional<int> opt;
optional<int> opt;
opt.emplace();
assert(static_cast<bool>(opt) == true);
assert(*opt == 0);
}
{
std::optional<int> opt;
optional<int> opt;
opt.emplace(1);
assert(static_cast<bool>(opt) == true);
assert(*opt == 1);
}
{
std::optional<int> opt(2);
optional<int> opt(2);
opt.emplace();
assert(static_cast<bool>(opt) == true);
assert(*opt == 0);
}
{
std::optional<int> opt(2);
optional<int> opt(2);
opt.emplace(1);
assert(static_cast<bool>(opt) == true);
assert(*opt == 1);
}
{
std::optional<X> opt;
optional<X> opt;
opt.emplace();
assert(static_cast<bool>(opt) == true);
assert(*opt == X());
}
{
std::optional<X> opt;
optional<X> opt;
opt.emplace(1);
assert(static_cast<bool>(opt) == true);
assert(*opt == X(1));
}
{
std::optional<X> opt;
optional<X> opt;
opt.emplace(1, 2);
assert(static_cast<bool>(opt) == true);
assert(*opt == X(1, 2));
}
{
std::optional<X> opt(X{3});
optional<X> opt(X{3});
opt.emplace();
assert(static_cast<bool>(opt) == true);
assert(*opt == X());
}
{
std::optional<X> opt(X{3});
optional<X> opt(X{3});
opt.emplace(1);
assert(static_cast<bool>(opt) == true);
assert(*opt == X(1));
}
{
std::optional<X> opt(X{3});
optional<X> opt(X{3});
opt.emplace(1, 2);
assert(static_cast<bool>(opt) == true);
assert(*opt == X(1, 2));
@ -120,7 +122,7 @@ int main()
{
Y y;
{
std::optional<Y> opt(y);
optional<Y> opt(y);
assert(Y::dtor_called == false);
opt.emplace();
assert(Y::dtor_called == true);
@ -128,7 +130,7 @@ int main()
}
{
Z z;
std::optional<Z> opt(z);
optional<Z> opt(z);
try
{
assert(static_cast<bool>(opt) == true);

View File

@ -12,13 +12,15 @@
// template <class U, class... Args>
// void optional<T>::emplace(initializer_list<U> il, Args&&... args);
#include <optional>
#include <experimental/optional>
#include <type_traits>
#include <cassert>
#include <vector>
#if _LIBCPP_STD_VER > 11
using std::experimental::optional;
class X
{
int i_;
@ -75,7 +77,7 @@ int main()
{
X x;
{
std::optional<X> opt(x);
optional<X> opt(x);
assert(X::dtor_called == false);
opt.emplace({1, 2});
assert(X::dtor_called == true);
@ -83,20 +85,20 @@ int main()
}
}
{
std::optional<std::vector<int>> opt;
optional<std::vector<int>> opt;
opt.emplace({1, 2, 3}, std::allocator<int>());
assert(static_cast<bool>(opt) == true);
assert(*opt == std::vector<int>({1, 2, 3}));
}
{
std::optional<Y> opt;
optional<Y> opt;
opt.emplace({1, 2});
assert(static_cast<bool>(opt) == true);
assert(*opt == Y({1, 2}));
}
{
Z z;
std::optional<Z> opt(z);
optional<Z> opt(z);
try
{
assert(static_cast<bool>(opt) == true);

View File

@ -13,12 +13,14 @@
// noexcept(is_nothrow_move_assignable<T>::value &&
// is_nothrow_move_constructible<T>::value);
#include <optional>
#include <experimental/optional>
#include <type_traits>
#include <cassert>
#if _LIBCPP_STD_VER > 11
using std::experimental::optional;
struct X
{
static bool throw_now;
@ -45,16 +47,16 @@ int main()
{
#if _LIBCPP_STD_VER > 11
{
static_assert(std::is_nothrow_move_assignable<std::optional<int>>::value, "");
std::optional<int> opt;
constexpr std::optional<int> opt2;
static_assert(std::is_nothrow_move_assignable<optional<int>>::value, "");
optional<int> opt;
constexpr optional<int> opt2;
opt = std::move(opt2);
static_assert(static_cast<bool>(opt2) == false, "");
assert(static_cast<bool>(opt) == static_cast<bool>(opt2));
}
{
std::optional<int> opt;
constexpr std::optional<int> opt2(2);
optional<int> opt;
constexpr optional<int> opt2(2);
opt = std::move(opt2);
static_assert(static_cast<bool>(opt2) == true, "");
static_assert(*opt2 == 2, "");
@ -62,15 +64,15 @@ int main()
assert(*opt == *opt2);
}
{
std::optional<int> opt(3);
constexpr std::optional<int> opt2;
optional<int> opt(3);
constexpr optional<int> opt2;
opt = std::move(opt2);
static_assert(static_cast<bool>(opt2) == false, "");
assert(static_cast<bool>(opt) == static_cast<bool>(opt2));
}
{
std::optional<int> opt(3);
constexpr std::optional<int> opt2(2);
optional<int> opt(3);
constexpr optional<int> opt2(2);
opt = std::move(opt2);
static_assert(static_cast<bool>(opt2) == true, "");
static_assert(*opt2 == 2, "");
@ -78,9 +80,9 @@ int main()
assert(*opt == *opt2);
}
{
static_assert(!std::is_nothrow_move_assignable<std::optional<X>>::value, "");
std::optional<X> opt;
std::optional<X> opt2(X{});
static_assert(!std::is_nothrow_move_assignable<optional<X>>::value, "");
optional<X> opt;
optional<X> opt2(X{});
assert(static_cast<bool>(opt2) == true);
try
{
@ -95,7 +97,7 @@ int main()
}
}
{
static_assert(std::is_nothrow_move_assignable<std::optional<Y>>::value, "");
static_assert(std::is_nothrow_move_assignable<optional<Y>>::value, "");
}
#endif // _LIBCPP_STD_VER > 11
}

View File

@ -11,12 +11,16 @@
// optional<T>& operator=(nullopt_t) noexcept;
#include <optional>
#include <experimental/optional>
#include <type_traits>
#include <cassert>
#if _LIBCPP_STD_VER > 11
using std::experimental::optional;
using std::experimental::nullopt_t;
using std::experimental::nullopt;
struct X
{
static bool dtor_called;
@ -31,30 +35,30 @@ int main()
{
#if _LIBCPP_STD_VER > 11
{
std::optional<int> opt;
static_assert(noexcept(opt = std::nullopt) == true, "");
opt = std::nullopt;
optional<int> opt;
static_assert(noexcept(opt = nullopt) == true, "");
opt = nullopt;
assert(static_cast<bool>(opt) == false);
}
{
std::optional<int> opt(3);
opt = std::nullopt;
optional<int> opt(3);
opt = nullopt;
assert(static_cast<bool>(opt) == false);
}
{
std::optional<X> opt;
static_assert(noexcept(opt = std::nullopt) == true, "");
optional<X> opt;
static_assert(noexcept(opt = nullopt) == true, "");
assert(X::dtor_called == false);
opt = std::nullopt;
opt = nullopt;
assert(X::dtor_called == false);
assert(static_cast<bool>(opt) == false);
}
{
X x;
{
std::optional<X> opt(x);
optional<X> opt(x);
assert(X::dtor_called == false);
opt = std::nullopt;
opt = nullopt;
assert(X::dtor_called == true);
assert(static_cast<bool>(opt) == false);
}

View File

@ -11,12 +11,14 @@
// constexpr optional(const T& v);
#include <optional>
#include <experimental/optional>
#include <type_traits>
#include <cassert>
#if _LIBCPP_STD_VER > 11
using std::experimental::optional;
class X
{
int i_;
@ -52,12 +54,12 @@ int main()
{
typedef int T;
constexpr T t(5);
constexpr std::optional<T> opt(t);
constexpr optional<T> opt(t);
static_assert(static_cast<bool>(opt) == true, "");
static_assert(*opt == 5, "");
struct test_constexpr_ctor
: public std::optional<T>
: public optional<T>
{
constexpr test_constexpr_ctor(const T&) {}
};
@ -66,12 +68,12 @@ int main()
{
typedef double T;
constexpr T t(3);
constexpr std::optional<T> opt(t);
constexpr optional<T> opt(t);
static_assert(static_cast<bool>(opt) == true, "");
static_assert(*opt == 3, "");
struct test_constexpr_ctor
: public std::optional<T>
: public optional<T>
{
constexpr test_constexpr_ctor(const T&) {}
};
@ -80,19 +82,19 @@ int main()
{
typedef X T;
const T t(3);
std::optional<T> opt(t);
optional<T> opt(t);
assert(static_cast<bool>(opt) == true);
assert(*opt == 3);
}
{
typedef Y T;
constexpr T t(3);
constexpr std::optional<T> opt(t);
constexpr optional<T> opt(t);
static_assert(static_cast<bool>(opt) == true, "");
static_assert(*opt == 3, "");
struct test_constexpr_ctor
: public std::optional<T>
: public optional<T>
{
constexpr test_constexpr_ctor(const T&) {}
};
@ -103,7 +105,7 @@ int main()
try
{
const T t(3);
std::optional<T> opt(t);
optional<T> opt(t);
assert(false);
}
catch (int i)

View File

@ -11,20 +11,22 @@
// optional(const optional<T>& rhs);
#include <optional>
#include <experimental/optional>
#include <type_traits>
#include <cassert>
#if _LIBCPP_STD_VER > 11
using std::experimental::optional;
template <class T>
void
test(const std::optional<T>& rhs, bool is_going_to_throw = false)
test(const optional<T>& rhs, bool is_going_to_throw = false)
{
bool rhs_engaged = static_cast<bool>(rhs);
try
{
std::optional<T> lhs = rhs;
optional<T> lhs = rhs;
assert(is_going_to_throw == false);
assert(static_cast<bool>(lhs) == rhs_engaged);
if (rhs_engaged)
@ -80,42 +82,42 @@ int main()
#if _LIBCPP_STD_VER > 11
{
typedef int T;
std::optional<T> rhs;
optional<T> rhs;
test(rhs);
}
{
typedef int T;
std::optional<T> rhs(3);
optional<T> rhs(3);
test(rhs);
}
{
typedef X T;
std::optional<T> rhs;
optional<T> rhs;
test(rhs);
}
{
typedef X T;
std::optional<T> rhs(X(3));
optional<T> rhs(X(3));
test(rhs);
}
{
typedef Y T;
std::optional<T> rhs;
optional<T> rhs;
test(rhs);
}
{
typedef Y T;
std::optional<T> rhs(Y(3));
optional<T> rhs(Y(3));
test(rhs);
}
{
typedef Z T;
std::optional<T> rhs;
optional<T> rhs;
test(rhs);
}
{
typedef Z T;
std::optional<T> rhs(Z(3));
optional<T> rhs(Z(3));
test(rhs, true);
}
#endif // _LIBCPP_STD_VER > 11

View File

@ -11,12 +11,14 @@
// constexpr optional() noexcept;
#include <optional>
#include <experimental/optional>
#include <type_traits>
#include <cassert>
#if _LIBCPP_STD_VER > 11
using std::experimental::optional;
template <class Opt>
void
test_constexpr()
@ -58,8 +60,8 @@ struct X
int main()
{
#if _LIBCPP_STD_VER > 11
test_constexpr<std::optional<int>>();
test_constexpr<std::optional<int*>>();
test<std::optional<X>>();
test_constexpr<optional<int>>();
test_constexpr<optional<int*>>();
test<optional<X>>();
#endif // _LIBCPP_STD_VER > 11
}

View File

@ -12,12 +12,16 @@
// template <class... Args>
// constexpr explicit optional(in_place_t, Args&&... args);
#include <optional>
#include <experimental/optional>
#include <type_traits>
#include <cassert>
#if _LIBCPP_STD_VER > 11
using std::experimental::optional;
using std::experimental::in_place_t;
using std::experimental::in_place;
class X
{
int i_;
@ -60,76 +64,76 @@ int main()
{
#if _LIBCPP_STD_VER > 11
{
constexpr std::optional<int> opt(std::in_place, 5);
constexpr optional<int> opt(in_place, 5);
static_assert(static_cast<bool>(opt) == true, "");
static_assert(*opt == 5, "");
struct test_constexpr_ctor
: public std::optional<int>
: public optional<int>
{
constexpr test_constexpr_ctor(std::in_place_t, int i)
: std::optional<int>(std::in_place, i) {}
constexpr test_constexpr_ctor(in_place_t, int i)
: optional<int>(in_place, i) {}
};
}
{
const std::optional<X> opt(std::in_place);
const optional<X> opt(in_place);
assert(static_cast<bool>(opt) == true);
assert(*opt == X());
}
{
const std::optional<X> opt(std::in_place, 5);
const optional<X> opt(in_place, 5);
assert(static_cast<bool>(opt) == true);
assert(*opt == X(5));
}
{
const std::optional<X> opt(std::in_place, 5, 4);
const optional<X> opt(in_place, 5, 4);
assert(static_cast<bool>(opt) == true);
assert(*opt == X(5, 4));
}
{
constexpr std::optional<Y> opt(std::in_place);
constexpr optional<Y> opt(in_place);
static_assert(static_cast<bool>(opt) == true, "");
static_assert(*opt == Y(), "");
struct test_constexpr_ctor
: public std::optional<Y>
: public optional<Y>
{
constexpr test_constexpr_ctor(std::in_place_t)
: std::optional<Y>(std::in_place) {}
constexpr test_constexpr_ctor(in_place_t)
: optional<Y>(in_place) {}
};
}
{
constexpr std::optional<Y> opt(std::in_place, 5);
constexpr optional<Y> opt(in_place, 5);
static_assert(static_cast<bool>(opt) == true, "");
static_assert(*opt == Y(5), "");
struct test_constexpr_ctor
: public std::optional<Y>
: public optional<Y>
{
constexpr test_constexpr_ctor(std::in_place_t, int i)
: std::optional<Y>(std::in_place, i) {}
constexpr test_constexpr_ctor(in_place_t, int i)
: optional<Y>(in_place, i) {}
};
}
{
constexpr std::optional<Y> opt(std::in_place, 5, 4);
constexpr optional<Y> opt(in_place, 5, 4);
static_assert(static_cast<bool>(opt) == true, "");
static_assert(*opt == Y(5, 4), "");
struct test_constexpr_ctor
: public std::optional<Y>
: public optional<Y>
{
constexpr test_constexpr_ctor(std::in_place_t, int i, int j)
: std::optional<Y>(std::in_place, i, j) {}
constexpr test_constexpr_ctor(in_place_t, int i, int j)
: optional<Y>(in_place, i, j) {}
};
}
{
try
{
const std::optional<Z> opt(std::in_place, 1);
const optional<Z> opt(in_place, 1);
assert(false);
}
catch (int i)

View File

@ -13,13 +13,17 @@
// constexpr
// explicit optional(in_place_t, initializer_list<U> il, Args&&... args);
#include <optional>
#include <experimental/optional>
#include <type_traits>
#include <vector>
#include <cassert>
#if _LIBCPP_STD_VER > 11
using std::experimental::optional;
using std::experimental::in_place_t;
using std::experimental::in_place;
class X
{
int i_;
@ -70,39 +74,39 @@ int main()
#if _LIBCPP_STD_VER > 11
{
static_assert(!std::is_constructible<X, std::initializer_list<int>&>::value, "");
static_assert(!std::is_constructible<std::optional<X>, std::initializer_list<int>&>::value, "");
static_assert(!std::is_constructible<optional<X>, std::initializer_list<int>&>::value, "");
}
{
std::optional<std::vector<int>> opt(std::in_place, {3, 1});
optional<std::vector<int>> opt(in_place, {3, 1});
assert(static_cast<bool>(opt) == true);
assert((*opt == std::vector<int>{3, 1}));
assert(opt->size() == 2);
}
{
std::optional<std::vector<int>> opt(std::in_place, {3, 1}, std::allocator<int>());
optional<std::vector<int>> opt(in_place, {3, 1}, std::allocator<int>());
assert(static_cast<bool>(opt) == true);
assert((*opt == std::vector<int>{3, 1}));
assert(opt->size() == 2);
}
{
static_assert(std::is_constructible<std::optional<Y>, std::initializer_list<int>&>::value, "");
constexpr std::optional<Y> opt(std::in_place, {3, 1});
static_assert(std::is_constructible<optional<Y>, std::initializer_list<int>&>::value, "");
constexpr optional<Y> opt(in_place, {3, 1});
static_assert(static_cast<bool>(opt) == true, "");
static_assert(*opt == Y{3, 1}, "");
struct test_constexpr_ctor
: public std::optional<Y>
: public optional<Y>
{
constexpr test_constexpr_ctor(std::in_place_t, std::initializer_list<int> i)
: std::optional<Y>(std::in_place, i) {}
constexpr test_constexpr_ctor(in_place_t, std::initializer_list<int> i)
: optional<Y>(in_place, i) {}
};
}
{
static_assert(std::is_constructible<std::optional<Z>, std::initializer_list<int>&>::value, "");
static_assert(std::is_constructible<optional<Z>, std::initializer_list<int>&>::value, "");
try
{
std::optional<Z> opt(std::in_place, {3, 1});
optional<Z> opt(in_place, {3, 1});
assert(false);
}
catch (int i)
@ -111,10 +115,10 @@ int main()
}
struct test_constexpr_ctor
: public std::optional<Z>
: public optional<Z>
{
constexpr test_constexpr_ctor(std::in_place_t, std::initializer_list<int> i)
: std::optional<Z>(std::in_place, i) {}
constexpr test_constexpr_ctor(in_place_t, std::initializer_list<int> i)
: optional<Z>(in_place, i) {}
};
}

View File

@ -11,22 +11,24 @@
// optional(optional<T>&& rhs) noexcept(is_nothrow_move_constructible<T>::value);
#include <optional>
#include <experimental/optional>
#include <type_traits>
#include <cassert>
#if _LIBCPP_STD_VER > 11
using std::experimental::optional;
template <class T>
void
test(std::optional<T>& rhs, bool is_going_to_throw = false)
test(optional<T>& rhs, bool is_going_to_throw = false)
{
static_assert(std::is_nothrow_move_constructible<std::optional<T>>::value ==
static_assert(std::is_nothrow_move_constructible<optional<T>>::value ==
std::is_nothrow_move_constructible<T>::value, "");
bool rhs_engaged = static_cast<bool>(rhs);
try
{
std::optional<T> lhs = std::move(rhs);
optional<T> lhs = std::move(rhs);
assert(is_going_to_throw == false);
assert(static_cast<bool>(lhs) == rhs_engaged);
}
@ -80,42 +82,42 @@ int main()
#if _LIBCPP_STD_VER > 11
{
typedef int T;
std::optional<T> rhs;
optional<T> rhs;
test(rhs);
}
{
typedef int T;
std::optional<T> rhs(3);
optional<T> rhs(3);
test(rhs);
}
{
typedef X T;
std::optional<T> rhs;
optional<T> rhs;
test(rhs);
}
{
typedef X T;
std::optional<T> rhs(X(3));
optional<T> rhs(X(3));
test(rhs);
}
{
typedef Y T;
std::optional<T> rhs;
optional<T> rhs;
test(rhs);
}
{
typedef Y T;
std::optional<T> rhs(Y(3));
optional<T> rhs(Y(3));
test(rhs);
}
{
typedef Z T;
std::optional<T> rhs;
optional<T> rhs;
test(rhs);
}
{
typedef Z T;
std::optional<T> rhs(Z(3));
optional<T> rhs(Z(3));
test(rhs, true);
}
#endif // _LIBCPP_STD_VER > 11

View File

@ -11,18 +11,22 @@
// constexpr optional(nullopt_t) noexcept;
#include <optional>
#include <experimental/optional>
#include <type_traits>
#include <cassert>
#if _LIBCPP_STD_VER > 11
using std::experimental::optional;
using std::experimental::nullopt_t;
using std::experimental::nullopt;
template <class Opt>
void
test_constexpr()
{
static_assert(noexcept(Opt(std::nullopt)), "");
constexpr Opt opt(std::nullopt);
static_assert(noexcept(Opt(nullopt)), "");
constexpr Opt opt(nullopt);
static_assert(static_cast<bool>(opt) == false, "");
struct test_constexpr_ctor
@ -36,8 +40,8 @@ template <class Opt>
void
test()
{
static_assert(noexcept(Opt(std::nullopt)), "");
Opt opt(std::nullopt);
static_assert(noexcept(Opt(nullopt)), "");
Opt opt(nullopt);
assert(static_cast<bool>(opt) == false);
struct test_constexpr_ctor
@ -57,8 +61,8 @@ struct X
int main()
{
#if _LIBCPP_STD_VER > 11
test_constexpr<std::optional<int>>();
test_constexpr<std::optional<int*>>();
test<std::optional<X>>();
test_constexpr<optional<int>>();
test_constexpr<optional<int*>>();
test<optional<X>>();
#endif // _LIBCPP_STD_VER > 11
}

View File

@ -11,12 +11,14 @@
// constexpr optional(T&& v);
#include <optional>
#include <experimental/optional>
#include <type_traits>
#include <cassert>
#if _LIBCPP_STD_VER > 11
using std::experimental::optional;
class X
{
int i_;
@ -52,42 +54,42 @@ int main()
#if _LIBCPP_STD_VER > 11
{
typedef int T;
constexpr std::optional<T> opt(T(5));
constexpr optional<T> opt(T(5));
static_assert(static_cast<bool>(opt) == true, "");
static_assert(*opt == 5, "");
struct test_constexpr_ctor
: public std::optional<T>
: public optional<T>
{
constexpr test_constexpr_ctor(T&&) {}
};
}
{
typedef double T;
constexpr std::optional<T> opt(T(3));
constexpr optional<T> opt(T(3));
static_assert(static_cast<bool>(opt) == true, "");
static_assert(*opt == 3, "");
struct test_constexpr_ctor
: public std::optional<T>
: public optional<T>
{
constexpr test_constexpr_ctor(T&&) {}
};
}
{
typedef X T;
std::optional<T> opt(T(3));
optional<T> opt(T(3));
assert(static_cast<bool>(opt) == true);
assert(*opt == 3);
}
{
typedef Y T;
constexpr std::optional<T> opt(T(3));
constexpr optional<T> opt(T(3));
static_assert(static_cast<bool>(opt) == true, "");
static_assert(*opt == 3, "");
struct test_constexpr_ctor
: public std::optional<T>
: public optional<T>
{
constexpr test_constexpr_ctor(T&&) {}
};
@ -96,7 +98,7 @@ int main()
typedef Z T;
try
{
std::optional<T> opt(T(3));
optional<T> opt(T(3));
assert(false);
}
catch (int i)

View File

@ -11,12 +11,14 @@
// ~optional();
#include <optional>
#include <experimental/optional>
#include <type_traits>
#include <cassert>
#if _LIBCPP_STD_VER > 11
using std::experimental::optional;
class X
{
public:
@ -35,20 +37,20 @@ int main()
{
typedef int T;
static_assert(std::is_trivially_destructible<T>::value, "");
static_assert(std::is_trivially_destructible<std::optional<T>>::value, "");
static_assert(std::is_trivially_destructible<optional<T>>::value, "");
}
{
typedef double T;
static_assert(std::is_trivially_destructible<T>::value, "");
static_assert(std::is_trivially_destructible<std::optional<T>>::value, "");
static_assert(std::is_trivially_destructible<optional<T>>::value, "");
}
{
typedef X T;
static_assert(!std::is_trivially_destructible<T>::value, "");
static_assert(!std::is_trivially_destructible<std::optional<T>>::value, "");
static_assert(!std::is_trivially_destructible<optional<T>>::value, "");
{
X x;
std::optional<X> opt{x};
optional<X> opt{x};
assert(X::dtor_called == false);
}
assert(X::dtor_called == true);

View File

@ -11,19 +11,21 @@
// constexpr explicit optional<T>::operator bool() const noexcept;
#include <optional>
#include <experimental/optional>
#include <type_traits>
#include <cassert>
int main()
{
#if _LIBCPP_STD_VER > 11
using std::experimental::optional;
{
constexpr std::optional<int> opt;
constexpr optional<int> opt;
static_assert(!opt, "");
}
{
constexpr std::optional<int> opt(0);
constexpr optional<int> opt(0);
static_assert(opt, "");
}
#endif // _LIBCPP_STD_VER > 11

View File

@ -15,12 +15,14 @@
#define _LIBCPP_ASSERT(x, m) ((x) ? (void)0 : std::exit(0))
#endif
#include <optional>
#include <experimental/optional>
#include <type_traits>
#include <cassert>
#if _LIBCPP_STD_VER > 11
using std::experimental::optional;
struct X
{
constexpr int test() const {return 3;}
@ -33,12 +35,12 @@ int main()
{
#if _LIBCPP_STD_VER > 11
{
std::optional<X> opt(X{});
optional<X> opt(X{});
assert((*opt).test() == 4);
}
#ifdef _LIBCPP_DEBUG
{
std::optional<X> opt;
optional<X> opt;
assert((*opt).test() == 3);
assert(false);
}

View File

@ -15,12 +15,14 @@
#define _LIBCPP_ASSERT(x, m) ((x) ? (void)0 : std::exit(0))
#endif
#include <optional>
#include <experimental/optional>
#include <type_traits>
#include <cassert>
#if _LIBCPP_STD_VER > 11
using std::experimental::optional;
struct X
{
constexpr int test() const {return 3;}
@ -37,16 +39,16 @@ int main()
{
#if _LIBCPP_STD_VER > 11
{
constexpr std::optional<X> opt(X{});
constexpr optional<X> opt(X{});
static_assert((*opt).test() == 3, "");
}
{
constexpr std::optional<Y> opt(Y{});
constexpr optional<Y> opt(Y{});
assert((*opt).test() == 2);
}
#ifdef _LIBCPP_DEBUG
{
const std::optional<X> opt;
const optional<X> opt;
assert((*opt).test() == 3);
assert(false);
}

View File

@ -15,12 +15,14 @@
#define _LIBCPP_ASSERT(x, m) ((x) ? (void)0 : std::exit(0))
#endif
#include <optional>
#include <experimental/optional>
#include <type_traits>
#include <cassert>
#if _LIBCPP_STD_VER > 11
using std::experimental::optional;
struct X
{
int test() const {return 2;}
@ -33,12 +35,12 @@ int main()
{
#if _LIBCPP_STD_VER > 11
{
std::optional<X> opt(X{});
optional<X> opt(X{});
assert(opt->test() == 3);
}
#ifdef _LIBCPP_DEBUG
{
std::optional<X> opt;
optional<X> opt;
assert(opt->test() == 3);
assert(false);
}

View File

@ -15,12 +15,14 @@
#define _LIBCPP_ASSERT(x, m) ((x) ? (void)0 : std::exit(0))
#endif
#include <optional>
#include <experimental/optional>
#include <type_traits>
#include <cassert>
#if _LIBCPP_STD_VER > 11
using std::experimental::optional;
struct X
{
constexpr int test() const {return 3;}
@ -43,20 +45,20 @@ int main()
{
#if _LIBCPP_STD_VER > 11
{
constexpr std::optional<X> opt(X{});
constexpr optional<X> opt(X{});
static_assert(opt->test() == 3, "");
}
{
constexpr std::optional<Y> opt(Y{});
constexpr optional<Y> opt(Y{});
assert(opt->test() == 2);
}
{
constexpr std::optional<Z> opt(Z{});
constexpr optional<Z> opt(Z{});
assert(opt->test() == 1);
}
#ifdef _LIBCPP_DEBUG
{
const std::optional<X> opt;
const optional<X> opt;
assert(opt->test() == 3);
assert(false);
}

View File

@ -11,12 +11,15 @@
// T& optional<T>::value();
#include <optional>
#include <experimental/optional>
#include <type_traits>
#include <cassert>
#if _LIBCPP_STD_VER > 11
using std::experimental::optional;
using std::experimental::bad_optional_access;
struct X
{
X() = default;
@ -31,18 +34,18 @@ int main()
{
#if _LIBCPP_STD_VER > 11
{
std::optional<X> opt;
optional<X> opt;
opt.emplace();
assert(opt.value().test() == 4);
}
{
std::optional<X> opt;
optional<X> opt;
try
{
opt.value();
assert(false);
}
catch (const std::bad_optional_access&)
catch (const bad_optional_access&)
{
}
}

View File

@ -11,12 +11,14 @@
// constexpr const T& optional<T>::value() const;
#include <optional>
#include <experimental/optional>
#include <type_traits>
#include <cassert>
#if _LIBCPP_STD_VER > 11
using std::experimental::optional;
struct X
{
constexpr int test() const {return 3;}
@ -29,7 +31,7 @@ int main()
{
#if _LIBCPP_STD_VER > 11
{
constexpr std::optional<X> opt;
constexpr optional<X> opt;
static_assert(opt.value().test() == 3, "");
}
#else

View File

@ -11,12 +11,17 @@
// constexpr const T& optional<T>::value() const;
#include <optional>
#include <experimental/optional>
#include <type_traits>
#include <cassert>
#if _LIBCPP_STD_VER > 11
using std::experimental::optional;
using std::experimental::in_place_t;
using std::experimental::in_place;
using std::experimental::bad_optional_access;
struct X
{
X() = default;
@ -31,21 +36,21 @@ int main()
{
#if _LIBCPP_STD_VER > 11
{
constexpr std::optional<X> opt(std::in_place);
constexpr optional<X> opt(in_place);
static_assert(opt.value().test() == 3, "");
}
{
const std::optional<X> opt(std::in_place);
const optional<X> opt(in_place);
assert(opt.value().test() == 3);
}
{
const std::optional<X> opt;
const optional<X> opt;
try
{
opt.value();
assert(false);
}
catch (const std::bad_optional_access&)
catch (const bad_optional_access&)
{
}
}

View File

@ -11,12 +11,16 @@
// template <class U> T optional<T>::value_or(U&& v) &&;
#include <optional>
#include <experimental/optional>
#include <type_traits>
#include <cassert>
#if _LIBCPP_STD_VER > 11
using std::experimental::optional;
using std::experimental::in_place_t;
using std::experimental::in_place;
struct Y
{
int i_;
@ -42,24 +46,24 @@ int main()
{
#if _LIBCPP_STD_VER > 11
{
std::optional<X> opt(std::in_place, 2);
optional<X> opt(in_place, 2);
Y y(3);
assert(std::move(opt).value_or(y) == 2);
assert(*opt == 0);
}
{
std::optional<X> opt(std::in_place, 2);
optional<X> opt(in_place, 2);
assert(std::move(opt).value_or(Y(3)) == 2);
assert(*opt == 0);
}
{
std::optional<X> opt;
optional<X> opt;
Y y(3);
assert(std::move(opt).value_or(y) == 3);
assert(!opt);
}
{
std::optional<X> opt;
optional<X> opt;
assert(std::move(opt).value_or(Y(3)) == 4);
assert(!opt);
}

View File

@ -11,12 +11,14 @@
// template <class U> constexpr T optional<T>::value_or(U&& v) const&;
#include <optional>
#include <experimental/optional>
#include <type_traits>
#include <cassert>
#if _LIBCPP_STD_VER > 11
using std::experimental::optional;
struct Y
{
int i_;
@ -41,39 +43,39 @@ int main()
{
#if _LIBCPP_STD_VER > 11
{
constexpr std::optional<X> opt(2);
constexpr optional<X> opt(2);
constexpr Y y(3);
static_assert(opt.value_or(y) == 2, "");
}
{
constexpr std::optional<X> opt(2);
constexpr optional<X> opt(2);
static_assert(opt.value_or(Y(3)) == 2, "");
}
{
constexpr std::optional<X> opt;
constexpr optional<X> opt;
constexpr Y y(3);
static_assert(opt.value_or(y) == 3, "");
}
{
constexpr std::optional<X> opt;
constexpr optional<X> opt;
static_assert(opt.value_or(Y(3)) == 4, "");
}
{
const std::optional<X> opt(2);
const optional<X> opt(2);
const Y y(3);
assert(opt.value_or(y) == 2);
}
{
const std::optional<X> opt(2);
const optional<X> opt(2);
assert(opt.value_or(Y(3)) == 2);
}
{
const std::optional<X> opt;
const optional<X> opt;
const Y y(3);
assert(opt.value_or(y) == 3);
}
{
const std::optional<X> opt;
const optional<X> opt;
assert(opt.value_or(Y(3)) == 4);
}
#endif // _LIBCPP_STD_VER > 11

View File

@ -13,12 +13,14 @@
// noexcept(is_nothrow_move_constructible<T>::value &&
// noexcept(swap(declval<T&>(), declval<T&>())));
#include <optional>
#include <experimental/optional>
#include <type_traits>
#include <cassert>
#if _LIBCPP_STD_VER > 11
using std::experimental::optional;
class X
{
int i_;
@ -67,8 +69,8 @@ int main()
{
#if _LIBCPP_STD_VER > 11
{
std::optional<int> opt1;
std::optional<int> opt2;
optional<int> opt1;
optional<int> opt2;
static_assert(noexcept(opt1.swap(opt2)) == true, "");
assert(static_cast<bool>(opt1) == false);
assert(static_cast<bool>(opt2) == false);
@ -77,8 +79,8 @@ int main()
assert(static_cast<bool>(opt2) == false);
}
{
std::optional<int> opt1(1);
std::optional<int> opt2;
optional<int> opt1(1);
optional<int> opt2;
static_assert(noexcept(opt1.swap(opt2)) == true, "");
assert(static_cast<bool>(opt1) == true);
assert(*opt1 == 1);
@ -89,8 +91,8 @@ int main()
assert(*opt2 == 1);
}
{
std::optional<int> opt1;
std::optional<int> opt2(2);
optional<int> opt1;
optional<int> opt2(2);
static_assert(noexcept(opt1.swap(opt2)) == true, "");
assert(static_cast<bool>(opt1) == false);
assert(static_cast<bool>(opt2) == true);
@ -101,8 +103,8 @@ int main()
assert(static_cast<bool>(opt2) == false);
}
{
std::optional<int> opt1(1);
std::optional<int> opt2(2);
optional<int> opt1(1);
optional<int> opt2(2);
static_assert(noexcept(opt1.swap(opt2)) == true, "");
assert(static_cast<bool>(opt1) == true);
assert(*opt1 == 1);
@ -115,8 +117,8 @@ int main()
assert(*opt2 == 1);
}
{
std::optional<X> opt1;
std::optional<X> opt2;
optional<X> opt1;
optional<X> opt2;
static_assert(noexcept(opt1.swap(opt2)) == true, "");
assert(static_cast<bool>(opt1) == false);
assert(static_cast<bool>(opt2) == false);
@ -126,8 +128,8 @@ int main()
assert(X::dtor_called == 0);
}
{
std::optional<X> opt1(1);
std::optional<X> opt2;
optional<X> opt1(1);
optional<X> opt2;
static_assert(noexcept(opt1.swap(opt2)) == true, "");
assert(static_cast<bool>(opt1) == true);
assert(*opt1 == 1);
@ -140,8 +142,8 @@ int main()
assert(*opt2 == 1);
}
{
std::optional<X> opt1;
std::optional<X> opt2(2);
optional<X> opt1;
optional<X> opt2(2);
static_assert(noexcept(opt1.swap(opt2)) == true, "");
assert(static_cast<bool>(opt1) == false);
assert(static_cast<bool>(opt2) == true);
@ -154,8 +156,8 @@ int main()
assert(static_cast<bool>(opt2) == false);
}
{
std::optional<X> opt1(1);
std::optional<X> opt2(2);
optional<X> opt1(1);
optional<X> opt2(2);
static_assert(noexcept(opt1.swap(opt2)) == true, "");
assert(static_cast<bool>(opt1) == true);
assert(*opt1 == 1);
@ -170,8 +172,8 @@ int main()
assert(*opt2 == 1);
}
{
std::optional<Y> opt1;
std::optional<Y> opt2;
optional<Y> opt1;
optional<Y> opt2;
static_assert(noexcept(opt1.swap(opt2)) == false, "");
assert(static_cast<bool>(opt1) == false);
assert(static_cast<bool>(opt2) == false);
@ -181,8 +183,8 @@ int main()
assert(Y::dtor_called == 0);
}
{
std::optional<Y> opt1(1);
std::optional<Y> opt2;
optional<Y> opt1(1);
optional<Y> opt2;
static_assert(noexcept(opt1.swap(opt2)) == false, "");
assert(static_cast<bool>(opt1) == true);
assert(*opt1 == 1);
@ -195,8 +197,8 @@ int main()
assert(*opt2 == 1);
}
{
std::optional<Y> opt1;
std::optional<Y> opt2(2);
optional<Y> opt1;
optional<Y> opt2(2);
static_assert(noexcept(opt1.swap(opt2)) == false, "");
assert(static_cast<bool>(opt1) == false);
assert(static_cast<bool>(opt2) == true);
@ -209,8 +211,8 @@ int main()
assert(static_cast<bool>(opt2) == false);
}
{
std::optional<Y> opt1(1);
std::optional<Y> opt2(2);
optional<Y> opt1(1);
optional<Y> opt2(2);
static_assert(noexcept(opt1.swap(opt2)) == false, "");
assert(static_cast<bool>(opt1) == true);
assert(*opt1 == 1);
@ -225,8 +227,8 @@ int main()
assert(*opt2 == 1);
}
{
std::optional<Z> opt1;
std::optional<Z> opt2;
optional<Z> opt1;
optional<Z> opt2;
static_assert(noexcept(opt1.swap(opt2)) == false, "");
assert(static_cast<bool>(opt1) == false);
assert(static_cast<bool>(opt2) == false);
@ -235,9 +237,9 @@ int main()
assert(static_cast<bool>(opt2) == false);
}
{
std::optional<Z> opt1;
optional<Z> opt1;
opt1.emplace(1);
std::optional<Z> opt2;
optional<Z> opt2;
static_assert(noexcept(opt1.swap(opt2)) == false, "");
assert(static_cast<bool>(opt1) == true);
assert(*opt1 == 1);
@ -256,8 +258,8 @@ int main()
assert(static_cast<bool>(opt2) == false);
}
{
std::optional<Z> opt1;
std::optional<Z> opt2;
optional<Z> opt1;
optional<Z> opt2;
opt2.emplace(2);
static_assert(noexcept(opt1.swap(opt2)) == false, "");
assert(static_cast<bool>(opt1) == false);
@ -277,9 +279,9 @@ int main()
assert(*opt2 == 2);
}
{
std::optional<Z> opt1;
optional<Z> opt1;
opt1.emplace(1);
std::optional<Z> opt2;
optional<Z> opt2;
opt2.emplace(2);
static_assert(noexcept(opt1.swap(opt2)) == false, "");
assert(static_cast<bool>(opt1) == true);

View File

@ -11,12 +11,14 @@
// T shall be an object type and shall satisfy the requirements of Destructible
#include <optional>
#include <experimental/optional>
int main()
{
#if _LIBCPP_STD_VER > 11
std::optional<const void> opt;
using std::experimental::optional;
optional<const void> opt;
#else
#error
#endif // _LIBCPP_STD_VER > 11

View File

@ -11,10 +11,12 @@
// T shall be an object type and shall satisfy the requirements of Destructible
#include <optional>
#include <experimental/optional>
#if _LIBCPP_STD_VER > 11
using std::experimental::optional;
struct X
{
private:
@ -26,7 +28,7 @@ private:
int main()
{
#if _LIBCPP_STD_VER > 11
std::optional<X> opt;
optional<X> opt;
#else
#error
#endif // _LIBCPP_STD_VER > 11

View File

@ -11,10 +11,12 @@
// T shall be an object type and shall satisfy the requirements of Destructible
#include <optional>
#include <experimental/optional>
#if _LIBCPP_STD_VER > 11
using std::experimental::optional;
struct X
{
~X() noexcept(false) {}
@ -25,7 +27,7 @@ struct X
int main()
{
#if _LIBCPP_STD_VER > 11
std::optional<X> opt;
optional<X> opt;
#else
#error
#endif // _LIBCPP_STD_VER > 11

View File

@ -11,12 +11,14 @@
// T shall be an object type and shall satisfy the requirements of Destructible
#include <optional>
#include <experimental/optional>
int main()
{
#if _LIBCPP_STD_VER > 11
std::optional<void> opt;
using std::experimental::optional;
optional<void> opt;
#else
#error
#endif // _LIBCPP_STD_VER > 11

View File

@ -16,11 +16,13 @@
// typedef T value_type;
// ...
#include <optional>
#include <experimental/optional>
#include <type_traits>
#if _LIBCPP_STD_VER > 11
using std::experimental::optional;
template <class Opt, class T>
void
test()
@ -33,9 +35,9 @@ test()
int main()
{
#if _LIBCPP_STD_VER > 11
test<std::optional<int>, int>();
test<std::optional<const int>, const int>();
test<std::optional<double>, double>();
test<std::optional<const double>, const double>();
test<optional<int>, int>();
test<optional<const int>, const int>();
test<optional<double>, double>();
test<optional<const double>, const double>();
#endif // _LIBCPP_STD_VER > 11
}

View File

@ -11,12 +11,14 @@
// template <class T> constexpr bool operator==(const optional<T>& x, const optional<T>& y);
#include <optional>
#include <experimental/optional>
#include <type_traits>
#include <cassert>
#if _LIBCPP_STD_VER > 11
using std::experimental::optional;
struct X
{
int i_;
@ -34,7 +36,7 @@ int main()
#if _LIBCPP_STD_VER > 11
{
typedef X T;
typedef std::optional<T> O;
typedef optional<T> O;
constexpr O o1; // disengaged
constexpr O o2; // disengaged

View File

@ -11,10 +11,12 @@
// template <class T> constexpr bool operator< (const optional<T>& x, const optional<T>& y);
#include <optional>
#include <experimental/optional>
#if _LIBCPP_STD_VER > 11
using std::experimental::optional;
struct X
{
int i_;
@ -31,7 +33,7 @@ int main()
{
#if _LIBCPP_STD_VER > 11
{
typedef std::optional<X> O;
typedef optional<X> O;
constexpr O o1; // disengaged
constexpr O o2; // disengaged

View File

@ -14,36 +14,35 @@
// optional<typename decay<T>::type>
// make_optional(T&& v);
#include <optional>
#include <experimental/optional>
#include <string>
#include <memory>
#include <cassert>
#if _LIBCPP_STD_VER > 11
#endif // _LIBCPP_STD_VER > 11
int main()
{
#if _LIBCPP_STD_VER > 11
using std::experimental::optional;
using std::experimental::make_optional;
{
std::optional<int> opt = std::make_optional(2);
optional<int> opt = make_optional(2);
assert(*opt == 2);
}
{
std::string s("123");
std::optional<std::string> opt = std::make_optional(s);
optional<std::string> opt = make_optional(s);
assert(*opt == s);
}
{
std::string s("123");
std::optional<std::string> opt = std::make_optional(std::move(s));
optional<std::string> opt = make_optional(std::move(s));
assert(*opt == "123");
assert(s.empty());
}
{
std::unique_ptr<int> s(new int(3));
std::optional<std::unique_ptr<int>> opt = std::make_optional(std::move(s));
optional<std::unique_ptr<int>> opt = make_optional(std::move(s));
assert(**opt == 3);
assert(s == nullptr);
}

View File

@ -12,12 +12,14 @@
// template <class T> void swap(optional<T>& x, optional<T>& y)
// noexcept(noexcept(x.swap(y)));
#include <optional>
#include <experimental/optional>
#include <type_traits>
#include <cassert>
#if _LIBCPP_STD_VER > 11
using std::experimental::optional;
class X
{
int i_;
@ -66,8 +68,8 @@ int main()
{
#if _LIBCPP_STD_VER > 11
{
std::optional<int> opt1;
std::optional<int> opt2;
optional<int> opt1;
optional<int> opt2;
static_assert(noexcept(swap(opt1, opt2)) == true, "");
assert(static_cast<bool>(opt1) == false);
assert(static_cast<bool>(opt2) == false);
@ -76,8 +78,8 @@ int main()
assert(static_cast<bool>(opt2) == false);
}
{
std::optional<int> opt1(1);
std::optional<int> opt2;
optional<int> opt1(1);
optional<int> opt2;
static_assert(noexcept(swap(opt1, opt2)) == true, "");
assert(static_cast<bool>(opt1) == true);
assert(*opt1 == 1);
@ -88,8 +90,8 @@ int main()
assert(*opt2 == 1);
}
{
std::optional<int> opt1;
std::optional<int> opt2(2);
optional<int> opt1;
optional<int> opt2(2);
static_assert(noexcept(swap(opt1, opt2)) == true, "");
assert(static_cast<bool>(opt1) == false);
assert(static_cast<bool>(opt2) == true);
@ -100,8 +102,8 @@ int main()
assert(static_cast<bool>(opt2) == false);
}
{
std::optional<int> opt1(1);
std::optional<int> opt2(2);
optional<int> opt1(1);
optional<int> opt2(2);
static_assert(noexcept(swap(opt1, opt2)) == true, "");
assert(static_cast<bool>(opt1) == true);
assert(*opt1 == 1);
@ -114,8 +116,8 @@ int main()
assert(*opt2 == 1);
}
{
std::optional<X> opt1;
std::optional<X> opt2;
optional<X> opt1;
optional<X> opt2;
static_assert(noexcept(swap(opt1, opt2)) == true, "");
assert(static_cast<bool>(opt1) == false);
assert(static_cast<bool>(opt2) == false);
@ -125,8 +127,8 @@ int main()
assert(X::dtor_called == 0);
}
{
std::optional<X> opt1(1);
std::optional<X> opt2;
optional<X> opt1(1);
optional<X> opt2;
static_assert(noexcept(swap(opt1, opt2)) == true, "");
assert(static_cast<bool>(opt1) == true);
assert(*opt1 == 1);
@ -139,8 +141,8 @@ int main()
assert(*opt2 == 1);
}
{
std::optional<X> opt1;
std::optional<X> opt2(2);
optional<X> opt1;
optional<X> opt2(2);
static_assert(noexcept(swap(opt1, opt2)) == true, "");
assert(static_cast<bool>(opt1) == false);
assert(static_cast<bool>(opt2) == true);
@ -153,8 +155,8 @@ int main()
assert(static_cast<bool>(opt2) == false);
}
{
std::optional<X> opt1(1);
std::optional<X> opt2(2);
optional<X> opt1(1);
optional<X> opt2(2);
static_assert(noexcept(swap(opt1, opt2)) == true, "");
assert(static_cast<bool>(opt1) == true);
assert(*opt1 == 1);
@ -169,8 +171,8 @@ int main()
assert(*opt2 == 1);
}
{
std::optional<Y> opt1;
std::optional<Y> opt2;
optional<Y> opt1;
optional<Y> opt2;
static_assert(noexcept(swap(opt1, opt2)) == false, "");
assert(static_cast<bool>(opt1) == false);
assert(static_cast<bool>(opt2) == false);
@ -180,8 +182,8 @@ int main()
assert(Y::dtor_called == 0);
}
{
std::optional<Y> opt1(1);
std::optional<Y> opt2;
optional<Y> opt1(1);
optional<Y> opt2;
static_assert(noexcept(swap(opt1, opt2)) == false, "");
assert(static_cast<bool>(opt1) == true);
assert(*opt1 == 1);
@ -194,8 +196,8 @@ int main()
assert(*opt2 == 1);
}
{
std::optional<Y> opt1;
std::optional<Y> opt2(2);
optional<Y> opt1;
optional<Y> opt2(2);
static_assert(noexcept(swap(opt1, opt2)) == false, "");
assert(static_cast<bool>(opt1) == false);
assert(static_cast<bool>(opt2) == true);
@ -208,8 +210,8 @@ int main()
assert(static_cast<bool>(opt2) == false);
}
{
std::optional<Y> opt1(1);
std::optional<Y> opt2(2);
optional<Y> opt1(1);
optional<Y> opt2(2);
static_assert(noexcept(swap(opt1, opt2)) == false, "");
assert(static_cast<bool>(opt1) == true);
assert(*opt1 == 1);
@ -224,8 +226,8 @@ int main()
assert(*opt2 == 1);
}
{
std::optional<Z> opt1;
std::optional<Z> opt2;
optional<Z> opt1;
optional<Z> opt2;
static_assert(noexcept(swap(opt1, opt2)) == false, "");
assert(static_cast<bool>(opt1) == false);
assert(static_cast<bool>(opt2) == false);
@ -234,9 +236,9 @@ int main()
assert(static_cast<bool>(opt2) == false);
}
{
std::optional<Z> opt1;
optional<Z> opt1;
opt1.emplace(1);
std::optional<Z> opt2;
optional<Z> opt2;
static_assert(noexcept(swap(opt1, opt2)) == false, "");
assert(static_cast<bool>(opt1) == true);
assert(*opt1 == 1);
@ -255,8 +257,8 @@ int main()
assert(static_cast<bool>(opt2) == false);
}
{
std::optional<Z> opt1;
std::optional<Z> opt2;
optional<Z> opt1;
optional<Z> opt2;
opt2.emplace(2);
static_assert(noexcept(swap(opt1, opt2)) == false, "");
assert(static_cast<bool>(opt1) == false);
@ -276,9 +278,9 @@ int main()
assert(*opt2 == 2);
}
{
std::optional<Z> opt1;
optional<Z> opt1;
opt1.emplace(1);
std::optional<Z> opt2;
optional<Z> opt2;
opt2.emplace(2);
static_assert(noexcept(swap(opt1, opt2)) == false, "");
assert(static_cast<bool>(opt1) == true);

View File

@ -12,12 +12,16 @@
// A program that necessitates the instantiation of template optional for
// (possibly cv-qualified) in_place_t is ill-formed.
#include <optional>
#include <experimental/optional>
int main()
{
#if _LIBCPP_STD_VER > 11
std::optional<const std::inplace_t> opt;
using std::experimental::optional;
using std::experimental::in_place_t;
using std::experimental::in_place;
optional<const in_place_t> opt;
#else
#error
#endif // _LIBCPP_STD_VER > 11

View File

@ -12,12 +12,14 @@
// A program that necessitates the instantiation of template optional for a
// reference type is ill-formed.
#include <optional>
#include <experimental/optional>
int main()
{
#if _LIBCPP_STD_VER > 11
std::optional<const int&> opt;
using std::experimental::optional;
optional<const int&> opt;
#else
#error
#endif // _LIBCPP_STD_VER > 11

View File

@ -12,12 +12,16 @@
// A program that necessitates the instantiation of template optional for
// (possibly cv-qualified) null_opt_t is ill-formed.
#include <optional>
#include <experimental/optional>
int main()
{
#if _LIBCPP_STD_VER > 11
std::optional<const std::nullopt_t> opt;
using std::experimental::optional;
using std::experimental::nullopt_t;
using std::experimental::nullopt;
optional<const nullopt_t> opt;
#else
#error
#endif // _LIBCPP_STD_VER > 11

View File

@ -12,12 +12,16 @@
// A program that necessitates the instantiation of template optional for
// (possibly cv-qualified) in_place_t is ill-formed.
#include <optional>
#include <experimental/optional>
int main()
{
#if _LIBCPP_STD_VER > 11
std::optional<std::inplace_t> opt;
using std::experimental::optional;
using std::experimental::in_place_t;
using std::experimental::in_place;
optional<in_place_t> opt;
#else
#error
#endif // _LIBCPP_STD_VER > 11

View File

@ -11,11 +11,13 @@
// #include <initializer_list>
#include <optional>
#include <experimental/optional>
int main()
{
#if _LIBCPP_STD_VER > 11
using std::experimental::optional;
std::initializer_list<int> list;
#endif // _LIBCPP_STD_VER > 11
}

View File

@ -12,12 +12,14 @@
// A program that necessitates the instantiation of template optional for a
// reference type is ill-formed.
#include <optional>
#include <experimental/optional>
int main()
{
#if _LIBCPP_STD_VER > 11
std::optional<int&> opt;
using std::experimental::optional;
optional<int&> opt;
#else
#error
#endif // _LIBCPP_STD_VER > 11

View File

@ -12,12 +12,16 @@
// A program that necessitates the instantiation of template optional for
// (possibly cv-qualified) null_opt_t is ill-formed.
#include <optional>
#include <experimental/optional>
int main()
{
#if _LIBCPP_STD_VER > 11
std::optional<std::nullopt_t> opt;
using std::experimental::optional;
using std::experimental::nullopt_t;
using std::experimental::nullopt;
optional<nullopt_t> opt;
#else
#error
#endif // _LIBCPP_STD_VER > 11

View File

@ -12,12 +12,14 @@
// A program that necessitates the instantiation of template optional for a
// reference type is ill-formed.
#include <optional>
#include <experimental/optional>
int main()
{
#if _LIBCPP_STD_VER > 11
std::optional<int&&> opt;
using std::experimental::optional;
optional<int&&> opt;
#else
#error
#endif // _LIBCPP_STD_VER > 11

View File

@ -9,7 +9,7 @@
// <optional>
#include <optional>
#include <experimental/optional>
#ifndef _LIBCPP_VERSION
#error _LIBCPP_VERSION not defined