Implement N3672, optional<T>.

git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@189772 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Howard Hinnant
2013-09-02 20:30:37 +00:00
parent c64c980140
commit 01afa5c6e4
65 changed files with 4535 additions and 4 deletions

View File

@@ -0,0 +1,70 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <optional>
// template <class U> optional<T>& operator=(U&& v);
#include <optional>
#include <type_traits>
#include <cassert>
#include <memory>
#if _LIBCPP_STD_VER > 11
struct X
{
};
#endif // _LIBCPP_STD_VER > 11
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, "");
{
std::optional<int> opt;
opt = 1;
assert(static_cast<bool>(opt) == true);
assert(*opt == 1);
}
{
std::optional<int> opt;
const int i = 2;
opt = i;
assert(static_cast<bool>(opt) == true);
assert(*opt == i);
}
{
std::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;
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)));
opt = std::unique_ptr<int>(new int(3));
assert(static_cast<bool>(opt) == true);
assert(**opt == 3);
}
#endif // _LIBCPP_STD_VER > 11
}

View File

@@ -0,0 +1,88 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <optional>
// optional<T>& operator=(const optional<T>& rhs);
#include <optional>
#include <type_traits>
#include <cassert>
#if _LIBCPP_STD_VER > 11
struct X
{
static bool throw_now;
X() = default;
X(const X&)
{
if (throw_now)
throw 6;
}
};
bool X::throw_now = false;
#endif // _LIBCPP_STD_VER > 11
int main()
{
#if _LIBCPP_STD_VER > 11
{
std::optional<int> opt;
constexpr std::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);
opt = opt2;
static_assert(static_cast<bool>(opt2) == true, "");
static_assert(*opt2 == 2, "");
assert(static_cast<bool>(opt) == static_cast<bool>(opt2));
assert(*opt == *opt2);
}
{
std::optional<int> opt(3);
constexpr std::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);
opt = opt2;
static_assert(static_cast<bool>(opt2) == true, "");
static_assert(*opt2 == 2, "");
assert(static_cast<bool>(opt) == static_cast<bool>(opt2));
assert(*opt == *opt2);
}
{
std::optional<X> opt;
std::optional<X> opt2(X{});
assert(static_cast<bool>(opt2) == true);
try
{
X::throw_now = true;
opt = opt2;
assert(false);
}
catch (int i)
{
assert(i == 6);
assert(static_cast<bool>(opt) == false);
}
}
#endif // _LIBCPP_STD_VER > 11
}

View File

@@ -0,0 +1,146 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <optional>
// template <class... Args> void optional<T>::emplace(Args&&... args);
#include <optional>
#include <type_traits>
#include <cassert>
#include <memory>
#if _LIBCPP_STD_VER > 11
class X
{
int i_;
int j_ = 0;
public:
X() : i_(0) {}
X(int i) : i_(i) {}
X(int i, int j) : i_(i), j_(j) {}
friend bool operator==(const X& x, const X& y)
{return x.i_ == y.i_ && x.j_ == y.j_;}
};
class Y
{
public:
static bool dtor_called;
Y() = default;
~Y() {dtor_called = true;}
};
bool Y::dtor_called = false;
class Z
{
public:
static bool dtor_called;
Z() = default;
Z(int) {throw 6;}
~Z() {dtor_called = true;}
};
bool Z::dtor_called = false;
#endif // _LIBCPP_STD_VER > 11
int main()
{
#if _LIBCPP_STD_VER > 11
{
std::optional<int> opt;
opt.emplace();
assert(static_cast<bool>(opt) == true);
assert(*opt == 0);
}
{
std::optional<int> opt;
opt.emplace(1);
assert(static_cast<bool>(opt) == true);
assert(*opt == 1);
}
{
std::optional<int> opt(2);
opt.emplace();
assert(static_cast<bool>(opt) == true);
assert(*opt == 0);
}
{
std::optional<int> opt(2);
opt.emplace(1);
assert(static_cast<bool>(opt) == true);
assert(*opt == 1);
}
{
std::optional<X> opt;
opt.emplace();
assert(static_cast<bool>(opt) == true);
assert(*opt == X());
}
{
std::optional<X> opt;
opt.emplace(1);
assert(static_cast<bool>(opt) == true);
assert(*opt == X(1));
}
{
std::optional<X> opt;
opt.emplace(1, 2);
assert(static_cast<bool>(opt) == true);
assert(*opt == X(1, 2));
}
{
std::optional<X> opt(X{3});
opt.emplace();
assert(static_cast<bool>(opt) == true);
assert(*opt == X());
}
{
std::optional<X> opt(X{3});
opt.emplace(1);
assert(static_cast<bool>(opt) == true);
assert(*opt == X(1));
}
{
std::optional<X> opt(X{3});
opt.emplace(1, 2);
assert(static_cast<bool>(opt) == true);
assert(*opt == X(1, 2));
}
{
Y y;
{
std::optional<Y> opt(y);
assert(Y::dtor_called == false);
opt.emplace();
assert(Y::dtor_called == true);
}
}
{
Z z;
std::optional<Z> opt(z);
try
{
assert(static_cast<bool>(opt) == true);
assert(Z::dtor_called == false);
opt.emplace(1);
}
catch (int i)
{
assert(i == 6);
assert(static_cast<bool>(opt) == false);
assert(Z::dtor_called == true);
}
}
#endif // _LIBCPP_STD_VER > 11
}

View File

@@ -0,0 +1,114 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <optional>
// template <class U, class... Args>
// void optional<T>::emplace(initializer_list<U> il, Args&&... args);
#include <optional>
#include <type_traits>
#include <cassert>
#include <vector>
#if _LIBCPP_STD_VER > 11
class X
{
int i_;
int j_ = 0;
public:
static bool dtor_called;
constexpr X() : i_(0) {}
constexpr X(int i) : i_(i) {}
constexpr X(std::initializer_list<int> il) : i_(il.begin()[0]), j_(il.begin()[1]) {}
~X() {dtor_called = true;}
friend constexpr bool operator==(const X& x, const X& y)
{return x.i_ == y.i_ && x.j_ == y.j_;}
};
bool X::dtor_called = false;
class Y
{
int i_;
int j_ = 0;
public:
constexpr Y() : i_(0) {}
constexpr Y(int i) : i_(i) {}
constexpr Y(std::initializer_list<int> il) : i_(il.begin()[0]), j_(il.begin()[1]) {}
friend constexpr bool operator==(const Y& x, const Y& y)
{return x.i_ == y.i_ && x.j_ == y.j_;}
};
class Z
{
int i_;
int j_ = 0;
public:
static bool dtor_called;
constexpr Z() : i_(0) {}
constexpr Z(int i) : i_(i) {}
constexpr Z(std::initializer_list<int> il) : i_(il.begin()[0]), j_(il.begin()[1])
{throw 6;}
~Z() {dtor_called = true;}
friend constexpr bool operator==(const Z& x, const Z& y)
{return x.i_ == y.i_ && x.j_ == y.j_;}
};
bool Z::dtor_called = false;
#endif // _LIBCPP_STD_VER > 11
int main()
{
#if _LIBCPP_STD_VER > 11
{
X x;
{
std::optional<X> opt(x);
assert(X::dtor_called == false);
opt.emplace({1, 2});
assert(X::dtor_called == true);
assert(*opt == X({1, 2}));
}
}
{
std::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;
opt.emplace({1, 2});
assert(static_cast<bool>(opt) == true);
assert(*opt == Y({1, 2}));
}
{
Z z;
std::optional<Z> opt(z);
try
{
assert(static_cast<bool>(opt) == true);
assert(Z::dtor_called == false);
opt.emplace({1, 2});
}
catch (int i)
{
assert(i == 6);
assert(static_cast<bool>(opt) == false);
assert(Z::dtor_called == true);
}
}
#endif // _LIBCPP_STD_VER > 11
}

View File

@@ -0,0 +1,101 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <optional>
// optional<T>& operator=(optional<T>&& rhs)
// noexcept(is_nothrow_move_assignable<T>::value &&
// is_nothrow_move_constructible<T>::value);
#include <optional>
#include <type_traits>
#include <cassert>
#if _LIBCPP_STD_VER > 11
struct X
{
static bool throw_now;
X() = default;
X(X&&)
{
if (throw_now)
throw 6;
}
X& operator=(X&&) noexcept
{
return *this;
}
};
struct Y {};
bool X::throw_now = false;
#endif // _LIBCPP_STD_VER > 11
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;
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);
opt = std::move(opt2);
static_assert(static_cast<bool>(opt2) == true, "");
static_assert(*opt2 == 2, "");
assert(static_cast<bool>(opt) == static_cast<bool>(opt2));
assert(*opt == *opt2);
}
{
std::optional<int> opt(3);
constexpr std::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);
opt = std::move(opt2);
static_assert(static_cast<bool>(opt2) == true, "");
static_assert(*opt2 == 2, "");
assert(static_cast<bool>(opt) == static_cast<bool>(opt2));
assert(*opt == *opt2);
}
{
static_assert(!std::is_nothrow_move_assignable<std::optional<X>>::value, "");
std::optional<X> opt;
std::optional<X> opt2(X{});
assert(static_cast<bool>(opt2) == true);
try
{
X::throw_now = true;
opt = std::move(opt2);
assert(false);
}
catch (int i)
{
assert(i == 6);
assert(static_cast<bool>(opt) == false);
}
}
{
static_assert(std::is_nothrow_move_assignable<std::optional<Y>>::value, "");
}
#endif // _LIBCPP_STD_VER > 11
}

View File

@@ -0,0 +1,63 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <optional>
// optional<T>& operator=(nullopt_t) noexcept;
#include <optional>
#include <type_traits>
#include <cassert>
#if _LIBCPP_STD_VER > 11
struct X
{
static bool dtor_called;
~X() {dtor_called = true;}
};
bool X::dtor_called = false;
#endif // _LIBCPP_STD_VER > 11
int main()
{
#if _LIBCPP_STD_VER > 11
{
std::optional<int> opt;
static_assert(noexcept(opt = std::nullopt) == true, "");
opt = std::nullopt;
assert(static_cast<bool>(opt) == false);
}
{
std::optional<int> opt(3);
opt = std::nullopt;
assert(static_cast<bool>(opt) == false);
}
{
std::optional<X> opt;
static_assert(noexcept(opt = std::nullopt) == true, "");
assert(X::dtor_called == false);
opt = std::nullopt;
assert(X::dtor_called == false);
assert(static_cast<bool>(opt) == false);
}
{
X x;
{
std::optional<X> opt(x);
assert(X::dtor_called == false);
opt = std::nullopt;
assert(X::dtor_called == true);
assert(static_cast<bool>(opt) == false);
}
}
#endif // _LIBCPP_STD_VER > 11
}

View File

@@ -0,0 +1,115 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <optional>
// constexpr optional(const T& v);
#include <optional>
#include <type_traits>
#include <cassert>
#if _LIBCPP_STD_VER > 11
class X
{
int i_;
public:
X(int i) : i_(i) {}
friend bool operator==(const X& x, const X& y) {return x.i_ == y.i_;}
};
class Y
{
int i_;
public:
constexpr Y(int i) : i_(i) {}
friend constexpr bool operator==(const Y& x, const Y& y) {return x.i_ == y.i_;}
};
class Z
{
int i_;
public:
Z(int i) : i_(i) {}
Z(const Z&) {throw 6;}
};
#endif // _LIBCPP_STD_VER > 11
int main()
{
#if _LIBCPP_STD_VER > 11
{
typedef int T;
constexpr T t(5);
constexpr std::optional<T> opt(t);
static_assert(static_cast<bool>(opt) == true, "");
static_assert(*opt == 5, "");
struct test_constexpr_ctor
: public std::optional<T>
{
constexpr test_constexpr_ctor(const T&) {}
};
}
{
typedef double T;
constexpr T t(3);
constexpr std::optional<T> opt(t);
static_assert(static_cast<bool>(opt) == true, "");
static_assert(*opt == 3, "");
struct test_constexpr_ctor
: public std::optional<T>
{
constexpr test_constexpr_ctor(const T&) {}
};
}
{
typedef X T;
const T t(3);
std::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);
static_assert(static_cast<bool>(opt) == true, "");
static_assert(*opt == 3, "");
struct test_constexpr_ctor
: public std::optional<T>
{
constexpr test_constexpr_ctor(const T&) {}
};
}
{
typedef Z T;
try
{
const T t(3);
std::optional<T> opt(t);
assert(false);
}
catch (int i)
{
assert(i == 6);
}
}
#endif // _LIBCPP_STD_VER > 11
}

View File

@@ -0,0 +1,122 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <optional>
// optional(const optional<T>& rhs);
#include <optional>
#include <type_traits>
#include <cassert>
#if _LIBCPP_STD_VER > 11
template <class T>
void
test(const std::optional<T>& rhs, bool is_going_to_throw = false)
{
bool rhs_engaged = static_cast<bool>(rhs);
try
{
std::optional<T> lhs = rhs;
assert(is_going_to_throw == false);
assert(static_cast<bool>(lhs) == rhs_engaged);
if (rhs_engaged)
assert(*lhs == *rhs);
}
catch (int i)
{
assert(i == 6);
}
}
class X
{
int i_;
public:
X(int i) : i_(i) {}
X(const X& x) : i_(x.i_) {}
~X() {i_ = 0;}
friend bool operator==(const X& x, const X& y) {return x.i_ == y.i_;}
};
class Y
{
int i_;
public:
Y(int i) : i_(i) {}
Y(const Y& x) : i_(x.i_) {}
friend constexpr bool operator==(const Y& x, const Y& y) {return x.i_ == y.i_;}
};
int count = 0;
class Z
{
int i_;
public:
Z(int i) : i_(i) {}
Z(const Z&)
{
if (++count == 2)
throw 6;
}
friend constexpr bool operator==(const Z& x, const Z& y) {return x.i_ == y.i_;}
};
#endif // _LIBCPP_STD_VER > 11
int main()
{
#if _LIBCPP_STD_VER > 11
{
typedef int T;
std::optional<T> rhs;
test(rhs);
}
{
typedef int T;
std::optional<T> rhs(3);
test(rhs);
}
{
typedef X T;
std::optional<T> rhs;
test(rhs);
}
{
typedef X T;
std::optional<T> rhs(X(3));
test(rhs);
}
{
typedef Y T;
std::optional<T> rhs;
test(rhs);
}
{
typedef Y T;
std::optional<T> rhs(Y(3));
test(rhs);
}
{
typedef Z T;
std::optional<T> rhs;
test(rhs);
}
{
typedef Z T;
std::optional<T> rhs(Z(3));
test(rhs, true);
}
#endif // _LIBCPP_STD_VER > 11
}

View File

@@ -0,0 +1,65 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <optional>
// constexpr optional() noexcept;
#include <optional>
#include <type_traits>
#include <cassert>
#if _LIBCPP_STD_VER > 11
template <class Opt>
void
test_constexpr()
{
static_assert(std::is_nothrow_default_constructible<Opt>::value, "");
constexpr Opt opt;
static_assert(static_cast<bool>(opt) == false, "");
struct test_constexpr_ctor
: public Opt
{
constexpr test_constexpr_ctor() {}
};
}
template <class Opt>
void
test()
{
static_assert(std::is_nothrow_default_constructible<Opt>::value, "");
Opt opt;
assert(static_cast<bool>(opt) == false);
struct test_constexpr_ctor
: public Opt
{
constexpr test_constexpr_ctor() {}
};
}
struct X
{
X();
};
#endif // _LIBCPP_STD_VER > 11
int main()
{
#if _LIBCPP_STD_VER > 11
test_constexpr<std::optional<int>>();
test_constexpr<std::optional<int*>>();
test<std::optional<X>>();
#endif // _LIBCPP_STD_VER > 11
}

View File

@@ -0,0 +1,141 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <optional>
// template <class... Args>
// constexpr explicit optional(in_place_t, Args&&... args);
#include <optional>
#include <type_traits>
#include <cassert>
#if _LIBCPP_STD_VER > 11
class X
{
int i_;
int j_ = 0;
public:
X() : i_(0) {}
X(int i) : i_(i) {}
X(int i, int j) : i_(i), j_(j) {}
~X() {}
friend bool operator==(const X& x, const X& y)
{return x.i_ == y.i_ && x.j_ == y.j_;}
};
class Y
{
int i_;
int j_ = 0;
public:
constexpr Y() : i_(0) {}
constexpr Y(int i) : i_(i) {}
constexpr Y(int i, int j) : i_(i), j_(j) {}
friend constexpr bool operator==(const Y& x, const Y& y)
{return x.i_ == y.i_ && x.j_ == y.j_;}
};
class Z
{
int i_;
public:
Z(int i) : i_(i) {throw 6;}
};
#endif // _LIBCPP_STD_VER > 11
int main()
{
#if _LIBCPP_STD_VER > 11
{
constexpr std::optional<int> opt(std::in_place, 5);
static_assert(static_cast<bool>(opt) == true, "");
static_assert(*opt == 5, "");
struct test_constexpr_ctor
: public std::optional<int>
{
constexpr test_constexpr_ctor(std::in_place_t, int i)
: std::optional<int>(std::in_place, i) {}
};
}
{
const std::optional<X> opt(std::in_place);
assert(static_cast<bool>(opt) == true);
assert(*opt == X());
}
{
const std::optional<X> opt(std::in_place, 5);
assert(static_cast<bool>(opt) == true);
assert(*opt == X(5));
}
{
const std::optional<X> opt(std::in_place, 5, 4);
assert(static_cast<bool>(opt) == true);
assert(*opt == X(5, 4));
}
{
constexpr std::optional<Y> opt(std::in_place);
static_assert(static_cast<bool>(opt) == true, "");
static_assert(*opt == Y(), "");
struct test_constexpr_ctor
: public std::optional<Y>
{
constexpr test_constexpr_ctor(std::in_place_t)
: std::optional<Y>(std::in_place) {}
};
}
{
constexpr std::optional<Y> opt(std::in_place, 5);
static_assert(static_cast<bool>(opt) == true, "");
static_assert(*opt == Y(5), "");
struct test_constexpr_ctor
: public std::optional<Y>
{
constexpr test_constexpr_ctor(std::in_place_t, int i)
: std::optional<Y>(std::in_place, i) {}
};
}
{
constexpr std::optional<Y> opt(std::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>
{
constexpr test_constexpr_ctor(std::in_place_t, int i, int j)
: std::optional<Y>(std::in_place, i, j) {}
};
}
{
try
{
const std::optional<Z> opt(std::in_place, 1);
assert(false);
}
catch (int i)
{
assert(i == 6);
}
}
#endif // _LIBCPP_STD_VER > 11
}

View File

@@ -0,0 +1,122 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <optional>
// template <class U, class... Args>
// constexpr
// explicit optional(in_place_t, initializer_list<U> il, Args&&... args);
#include <optional>
#include <type_traits>
#include <vector>
#include <cassert>
#if _LIBCPP_STD_VER > 11
class X
{
int i_;
int j_ = 0;
public:
X() : i_(0) {}
X(int i) : i_(i) {}
X(int i, int j) : i_(i), j_(j) {}
~X() {}
friend bool operator==(const X& x, const X& y)
{return x.i_ == y.i_ && x.j_ == y.j_;}
};
class Y
{
int i_;
int j_ = 0;
public:
constexpr Y() : i_(0) {}
constexpr Y(int i) : i_(i) {}
constexpr Y(std::initializer_list<int> il) : i_(il.begin()[0]), j_(il.begin()[1]) {}
friend constexpr bool operator==(const Y& x, const Y& y)
{return x.i_ == y.i_ && x.j_ == y.j_;}
};
class Z
{
int i_;
int j_ = 0;
public:
constexpr Z() : i_(0) {}
constexpr Z(int i) : i_(i) {}
constexpr Z(std::initializer_list<int> il) : i_(il.begin()[0]), j_(il.begin()[1])
{throw 6;}
friend constexpr bool operator==(const Z& x, const Z& y)
{return x.i_ == y.i_ && x.j_ == y.j_;}
};
#endif // _LIBCPP_STD_VER > 11
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, "");
}
{
std::optional<std::vector<int>> opt(std::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>());
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(static_cast<bool>(opt) == true, "");
static_assert(*opt == Y{3, 1}, "");
struct test_constexpr_ctor
: public std::optional<Y>
{
constexpr test_constexpr_ctor(std::in_place_t, std::initializer_list<int> i)
: std::optional<Y>(std::in_place, i) {}
};
}
{
static_assert(std::is_constructible<std::optional<Z>, std::initializer_list<int>&>::value, "");
try
{
std::optional<Z> opt(std::in_place, {3, 1});
assert(false);
}
catch (int i)
{
assert(i == 6);
}
struct test_constexpr_ctor
: public std::optional<Z>
{
constexpr test_constexpr_ctor(std::in_place_t, std::initializer_list<int> i)
: std::optional<Z>(std::in_place, i) {}
};
}
#endif // _LIBCPP_STD_VER > 11
}

View File

@@ -0,0 +1,122 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <optional>
// optional(optional<T>&& rhs) noexcept(is_nothrow_move_constructible<T>::value);
#include <optional>
#include <type_traits>
#include <cassert>
#if _LIBCPP_STD_VER > 11
template <class T>
void
test(std::optional<T>& rhs, bool is_going_to_throw = false)
{
static_assert(std::is_nothrow_move_constructible<std::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);
assert(is_going_to_throw == false);
assert(static_cast<bool>(lhs) == rhs_engaged);
}
catch (int i)
{
assert(i == 6);
}
}
class X
{
int i_;
public:
X(int i) : i_(i) {}
X(X&& x) : i_(x.i_) {x.i_ = 0;}
~X() {i_ = 0;}
friend bool operator==(const X& x, const X& y) {return x.i_ == y.i_;}
};
class Y
{
int i_;
public:
Y(int i) : i_(i) {}
Y(Y&& x) noexcept : i_(x.i_) {x.i_ = 0;}
friend constexpr bool operator==(const Y& x, const Y& y) {return x.i_ == y.i_;}
};
int count = 0;
class Z
{
int i_;
public:
Z(int i) : i_(i) {}
Z(Z&&)
{
if (++count == 2)
throw 6;
}
friend constexpr bool operator==(const Z& x, const Z& y) {return x.i_ == y.i_;}
};
#endif // _LIBCPP_STD_VER > 11
int main()
{
#if _LIBCPP_STD_VER > 11
{
typedef int T;
std::optional<T> rhs;
test(rhs);
}
{
typedef int T;
std::optional<T> rhs(3);
test(rhs);
}
{
typedef X T;
std::optional<T> rhs;
test(rhs);
}
{
typedef X T;
std::optional<T> rhs(X(3));
test(rhs);
}
{
typedef Y T;
std::optional<T> rhs;
test(rhs);
}
{
typedef Y T;
std::optional<T> rhs(Y(3));
test(rhs);
}
{
typedef Z T;
std::optional<T> rhs;
test(rhs);
}
{
typedef Z T;
std::optional<T> rhs(Z(3));
test(rhs, true);
}
#endif // _LIBCPP_STD_VER > 11
}

View File

@@ -0,0 +1,64 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <optional>
// constexpr optional(nullopt_t) noexcept;
#include <optional>
#include <type_traits>
#include <cassert>
#if _LIBCPP_STD_VER > 11
template <class Opt>
void
test_constexpr()
{
static_assert(noexcept(Opt(std::nullopt)), "");
constexpr Opt opt(std::nullopt);
static_assert(static_cast<bool>(opt) == false, "");
struct test_constexpr_ctor
: public Opt
{
constexpr test_constexpr_ctor() {}
};
}
template <class Opt>
void
test()
{
static_assert(noexcept(Opt(std::nullopt)), "");
Opt opt(std::nullopt);
assert(static_cast<bool>(opt) == false);
struct test_constexpr_ctor
: public Opt
{
constexpr test_constexpr_ctor() {}
};
}
struct X
{
X();
};
#endif // _LIBCPP_STD_VER > 11
int main()
{
#if _LIBCPP_STD_VER > 11
test_constexpr<std::optional<int>>();
test_constexpr<std::optional<int*>>();
test<std::optional<X>>();
#endif // _LIBCPP_STD_VER > 11
}

View File

@@ -0,0 +1,108 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <optional>
// constexpr optional(T&& v);
#include <optional>
#include <type_traits>
#include <cassert>
#if _LIBCPP_STD_VER > 11
class X
{
int i_;
public:
X(int i) : i_(i) {}
X(X&& x) : i_(x.i_) {}
friend bool operator==(const X& x, const X& y) {return x.i_ == y.i_;}
};
class Y
{
int i_;
public:
constexpr Y(int i) : i_(i) {}
constexpr Y(Y&& x) : i_(x.i_) {}
friend constexpr bool operator==(const Y& x, const Y& y) {return x.i_ == y.i_;}
};
class Z
{
int i_;
public:
Z(int i) : i_(i) {}
Z(Z&&) {throw 6;}
};
#endif // _LIBCPP_STD_VER > 11
int main()
{
#if _LIBCPP_STD_VER > 11
{
typedef int T;
constexpr std::optional<T> opt(T(5));
static_assert(static_cast<bool>(opt) == true, "");
static_assert(*opt == 5, "");
struct test_constexpr_ctor
: public std::optional<T>
{
constexpr test_constexpr_ctor(T&&) {}
};
}
{
typedef double T;
constexpr std::optional<T> opt(T(3));
static_assert(static_cast<bool>(opt) == true, "");
static_assert(*opt == 3, "");
struct test_constexpr_ctor
: public std::optional<T>
{
constexpr test_constexpr_ctor(T&&) {}
};
}
{
typedef X T;
std::optional<T> opt(T(3));
assert(static_cast<bool>(opt) == true);
assert(*opt == 3);
}
{
typedef Y T;
constexpr std::optional<T> opt(T(3));
static_assert(static_cast<bool>(opt) == true, "");
static_assert(*opt == 3, "");
struct test_constexpr_ctor
: public std::optional<T>
{
constexpr test_constexpr_ctor(T&&) {}
};
}
{
typedef Z T;
try
{
std::optional<T> opt(T(3));
assert(false);
}
catch (int i)
{
assert(i == 6);
}
}
#endif // _LIBCPP_STD_VER > 11
}

View File

@@ -0,0 +1,57 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <optional>
// ~optional();
#include <optional>
#include <type_traits>
#include <cassert>
#if _LIBCPP_STD_VER > 11
class X
{
public:
static bool dtor_called;
X() = default;
~X() {dtor_called = true;}
};
bool X::dtor_called = false;
#endif // _LIBCPP_STD_VER > 11
int main()
{
#if _LIBCPP_STD_VER > 11
{
typedef int T;
static_assert(std::is_trivially_destructible<T>::value, "");
static_assert(std::is_trivially_destructible<std::optional<T>>::value, "");
}
{
typedef double T;
static_assert(std::is_trivially_destructible<T>::value, "");
static_assert(std::is_trivially_destructible<std::optional<T>>::value, "");
}
{
typedef X T;
static_assert(!std::is_trivially_destructible<T>::value, "");
static_assert(!std::is_trivially_destructible<std::optional<T>>::value, "");
{
X x;
std::optional<X> opt{x};
assert(X::dtor_called == false);
}
assert(X::dtor_called == true);
}
#endif // _LIBCPP_STD_VER > 11
}

View File

@@ -0,0 +1,30 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <optional>
// constexpr explicit optional<T>::operator bool() const noexcept;
#include <optional>
#include <type_traits>
#include <cassert>
int main()
{
#if _LIBCPP_STD_VER > 11
{
constexpr std::optional<int> opt;
static_assert(!opt, "");
}
{
constexpr std::optional<int> opt(0);
static_assert(opt, "");
}
#endif // _LIBCPP_STD_VER > 11
}

View File

@@ -0,0 +1,47 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <optional>
// T& optional<T>::operator*();
#ifdef _LIBCPP_DEBUG
#define _LIBCPP_ASSERT(x, m) ((x) ? (void)0 : std::exit(0))
#endif
#include <optional>
#include <type_traits>
#include <cassert>
#if _LIBCPP_STD_VER > 11
struct X
{
constexpr int test() const {return 3;}
int test() {return 4;}
};
#endif // _LIBCPP_STD_VER > 11
int main()
{
#if _LIBCPP_STD_VER > 11
{
std::optional<X> opt(X{});
assert((*opt).test() == 4);
}
#ifdef _LIBCPP_DEBUG
{
std::optional<X> opt;
assert((*opt).test() == 3);
assert(false);
}
#endif // _LIBCPP_DEBUG
#endif // _LIBCPP_STD_VER > 11
}

View File

@@ -0,0 +1,55 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <optional>
// constexpr const T& optional<T>::operator*() const;
#ifdef _LIBCPP_DEBUG
#define _LIBCPP_ASSERT(x, m) ((x) ? (void)0 : std::exit(0))
#endif
#include <optional>
#include <type_traits>
#include <cassert>
#if _LIBCPP_STD_VER > 11
struct X
{
constexpr int test() const {return 3;}
};
struct Y
{
int test() const {return 2;}
};
#endif // _LIBCPP_STD_VER > 11
int main()
{
#if _LIBCPP_STD_VER > 11
{
constexpr std::optional<X> opt(X{});
static_assert((*opt).test() == 3, "");
}
{
constexpr std::optional<Y> opt(Y{});
assert((*opt).test() == 2);
}
#ifdef _LIBCPP_DEBUG
{
const std::optional<X> opt;
assert((*opt).test() == 3);
assert(false);
}
#endif // _LIBCPP_DEBUG
#endif // _LIBCPP_STD_VER > 11
}

View File

@@ -0,0 +1,47 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <optional>
// T* optional<T>::operator->();
#ifdef _LIBCPP_DEBUG
#define _LIBCPP_ASSERT(x, m) ((x) ? (void)0 : std::exit(0))
#endif
#include <optional>
#include <type_traits>
#include <cassert>
#if _LIBCPP_STD_VER > 11
struct X
{
int test() const {return 2;}
int test() {return 3;}
};
#endif // _LIBCPP_STD_VER > 11
int main()
{
#if _LIBCPP_STD_VER > 11
{
std::optional<X> opt(X{});
assert(opt->test() == 3);
}
#ifdef _LIBCPP_DEBUG
{
std::optional<X> opt;
assert(opt->test() == 3);
assert(false);
}
#endif // _LIBCPP_DEBUG
#endif // _LIBCPP_STD_VER > 11
}

View File

@@ -0,0 +1,65 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <optional>
// constexpr const T* optional<T>::operator->() const;
#ifdef _LIBCPP_DEBUG
#define _LIBCPP_ASSERT(x, m) ((x) ? (void)0 : std::exit(0))
#endif
#include <optional>
#include <type_traits>
#include <cassert>
#if _LIBCPP_STD_VER > 11
struct X
{
constexpr int test() const {return 3;}
};
struct Y
{
int test() const {return 2;}
};
struct Z
{
const Z* operator&() const {return this;}
constexpr int test() const {return 1;}
};
#endif // _LIBCPP_STD_VER > 11
int main()
{
#if _LIBCPP_STD_VER > 11
{
constexpr std::optional<X> opt(X{});
static_assert(opt->test() == 3, "");
}
{
constexpr std::optional<Y> opt(Y{});
assert(opt->test() == 2);
}
{
constexpr std::optional<Z> opt(Z{});
assert(opt->test() == 1);
}
#ifdef _LIBCPP_DEBUG
{
const std::optional<X> opt;
assert(opt->test() == 3);
assert(false);
}
#endif // _LIBCPP_DEBUG
#endif // _LIBCPP_STD_VER > 11
}

View File

@@ -0,0 +1,50 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <optional>
// T& optional<T>::value();
#include <optional>
#include <type_traits>
#include <cassert>
#if _LIBCPP_STD_VER > 11
struct X
{
X() = default;
X(const X&) = delete;
constexpr int test() const {return 3;}
int test() {return 4;}
};
#endif // _LIBCPP_STD_VER > 11
int main()
{
#if _LIBCPP_STD_VER > 11
{
std::optional<X> opt;
opt.emplace();
assert(opt.value().test() == 4);
}
{
std::optional<X> opt;
try
{
opt.value();
assert(false);
}
catch (const std::bad_optional_access&)
{
}
}
#endif // _LIBCPP_STD_VER > 11
}

View File

@@ -0,0 +1,38 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <optional>
// constexpr const T& optional<T>::value() const;
#include <optional>
#include <type_traits>
#include <cassert>
#if _LIBCPP_STD_VER > 11
struct X
{
constexpr int test() const {return 3;}
int test() {return 4;}
};
#endif // _LIBCPP_STD_VER > 11
int main()
{
#if _LIBCPP_STD_VER > 11
{
constexpr std::optional<X> opt;
static_assert(opt.value().test() == 3, "");
}
#else
#error
#endif // _LIBCPP_STD_VER > 11
}

View File

@@ -0,0 +1,53 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <optional>
// constexpr const T& optional<T>::value() const;
#include <optional>
#include <type_traits>
#include <cassert>
#if _LIBCPP_STD_VER > 11
struct X
{
X() = default;
X(const X&) = delete;
constexpr int test() const {return 3;}
int test() {return 4;}
};
#endif // _LIBCPP_STD_VER > 11
int main()
{
#if _LIBCPP_STD_VER > 11
{
constexpr std::optional<X> opt(std::in_place);
static_assert(opt.value().test() == 3, "");
}
{
const std::optional<X> opt(std::in_place);
assert(opt.value().test() == 3);
}
{
const std::optional<X> opt;
try
{
opt.value();
assert(false);
}
catch (const std::bad_optional_access&)
{
}
}
#endif // _LIBCPP_STD_VER > 11
}

View File

@@ -0,0 +1,67 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <optional>
// template <class U> T optional<T>::value_or(U&& v) &&;
#include <optional>
#include <type_traits>
#include <cassert>
#if _LIBCPP_STD_VER > 11
struct Y
{
int i_;
Y(int i) : i_(i) {}
};
struct X
{
int i_;
X(int i) : i_(i) {}
X(X&& x) : i_(x.i_) {x.i_ = 0;}
X(const Y& y) : i_(y.i_) {}
X(Y&& y) : i_(y.i_+1) {}
friend constexpr bool operator==(const X& x, const X& y)
{return x.i_ == y.i_;}
};
#endif // _LIBCPP_STD_VER > 11
int main()
{
#if _LIBCPP_STD_VER > 11
{
std::optional<X> opt(std::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);
assert(std::move(opt).value_or(Y(3)) == 2);
assert(*opt == 0);
}
{
std::optional<X> opt;
Y y(3);
assert(std::move(opt).value_or(y) == 3);
assert(!opt);
}
{
std::optional<X> opt;
assert(std::move(opt).value_or(Y(3)) == 4);
assert(!opt);
}
#endif // _LIBCPP_STD_VER > 11
}

View File

@@ -0,0 +1,80 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <optional>
// template <class U> constexpr T optional<T>::value_or(U&& v) const&;
#include <optional>
#include <type_traits>
#include <cassert>
#if _LIBCPP_STD_VER > 11
struct Y
{
int i_;
constexpr Y(int i) : i_(i) {}
};
struct X
{
int i_;
constexpr X(int i) : i_(i) {}
constexpr X(const Y& y) : i_(y.i_) {}
constexpr X(Y&& y) : i_(y.i_+1) {}
friend constexpr bool operator==(const X& x, const X& y)
{return x.i_ == y.i_;}
};
#endif // _LIBCPP_STD_VER > 11
int main()
{
#if _LIBCPP_STD_VER > 11
{
constexpr std::optional<X> opt(2);
constexpr Y y(3);
static_assert(opt.value_or(y) == 2, "");
}
{
constexpr std::optional<X> opt(2);
static_assert(opt.value_or(Y(3)) == 2, "");
}
{
constexpr std::optional<X> opt;
constexpr Y y(3);
static_assert(opt.value_or(y) == 3, "");
}
{
constexpr std::optional<X> opt;
static_assert(opt.value_or(Y(3)) == 4, "");
}
{
const std::optional<X> opt(2);
const Y y(3);
assert(opt.value_or(y) == 2);
}
{
const std::optional<X> opt(2);
assert(opt.value_or(Y(3)) == 2);
}
{
const std::optional<X> opt;
const Y y(3);
assert(opt.value_or(y) == 3);
}
{
const std::optional<X> opt;
assert(opt.value_or(Y(3)) == 4);
}
#endif // _LIBCPP_STD_VER > 11
}

View File

@@ -0,0 +1,304 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <optional>
// void swap(optional&)
// noexcept(is_nothrow_move_constructible<T>::value &&
// noexcept(swap(declval<T&>(), declval<T&>())));
#include <optional>
#include <type_traits>
#include <cassert>
#if _LIBCPP_STD_VER > 11
class X
{
int i_;
public:
static unsigned dtor_called;
X(int i) : i_(i) {}
X(X&& x) = default;
X& operator=(X&&) = default;
~X() {++dtor_called;}
friend bool operator==(const X& x, const X& y) {return x.i_ == y.i_;}
};
unsigned X::dtor_called = 0;
class Y
{
int i_;
public:
static unsigned dtor_called;
Y(int i) : i_(i) {}
Y(Y&&) = default;
~Y() {++dtor_called;}
friend constexpr bool operator==(const Y& x, const Y& y) {return x.i_ == y.i_;}
friend void swap(Y& x, Y& y) {std::swap(x.i_, y.i_);}
};
unsigned Y::dtor_called = 0;
class Z
{
int i_;
public:
Z(int i) : i_(i) {}
Z(Z&&) {throw 7;}
friend constexpr bool operator==(const Z& x, const Z& y) {return x.i_ == y.i_;}
friend void swap(Z& x, Z& y) {throw 6;}
};
#endif // _LIBCPP_STD_VER > 11
int main()
{
#if _LIBCPP_STD_VER > 11
{
std::optional<int> opt1;
std::optional<int> opt2;
static_assert(noexcept(opt1.swap(opt2)) == true, "");
assert(static_cast<bool>(opt1) == false);
assert(static_cast<bool>(opt2) == false);
opt1.swap(opt2);
assert(static_cast<bool>(opt1) == false);
assert(static_cast<bool>(opt2) == false);
}
{
std::optional<int> opt1(1);
std::optional<int> opt2;
static_assert(noexcept(opt1.swap(opt2)) == true, "");
assert(static_cast<bool>(opt1) == true);
assert(*opt1 == 1);
assert(static_cast<bool>(opt2) == false);
opt1.swap(opt2);
assert(static_cast<bool>(opt1) == false);
assert(static_cast<bool>(opt2) == true);
assert(*opt2 == 1);
}
{
std::optional<int> opt1;
std::optional<int> opt2(2);
static_assert(noexcept(opt1.swap(opt2)) == true, "");
assert(static_cast<bool>(opt1) == false);
assert(static_cast<bool>(opt2) == true);
assert(*opt2 == 2);
opt1.swap(opt2);
assert(static_cast<bool>(opt1) == true);
assert(*opt1 == 2);
assert(static_cast<bool>(opt2) == false);
}
{
std::optional<int> opt1(1);
std::optional<int> opt2(2);
static_assert(noexcept(opt1.swap(opt2)) == true, "");
assert(static_cast<bool>(opt1) == true);
assert(*opt1 == 1);
assert(static_cast<bool>(opt2) == true);
assert(*opt2 == 2);
opt1.swap(opt2);
assert(static_cast<bool>(opt1) == true);
assert(*opt1 == 2);
assert(static_cast<bool>(opt2) == true);
assert(*opt2 == 1);
}
{
std::optional<X> opt1;
std::optional<X> opt2;
static_assert(noexcept(opt1.swap(opt2)) == true, "");
assert(static_cast<bool>(opt1) == false);
assert(static_cast<bool>(opt2) == false);
opt1.swap(opt2);
assert(static_cast<bool>(opt1) == false);
assert(static_cast<bool>(opt2) == false);
assert(X::dtor_called == 0);
}
{
std::optional<X> opt1(1);
std::optional<X> opt2;
static_assert(noexcept(opt1.swap(opt2)) == true, "");
assert(static_cast<bool>(opt1) == true);
assert(*opt1 == 1);
assert(static_cast<bool>(opt2) == false);
X::dtor_called = 0;
opt1.swap(opt2);
assert(X::dtor_called == 1);
assert(static_cast<bool>(opt1) == false);
assert(static_cast<bool>(opt2) == true);
assert(*opt2 == 1);
}
{
std::optional<X> opt1;
std::optional<X> opt2(2);
static_assert(noexcept(opt1.swap(opt2)) == true, "");
assert(static_cast<bool>(opt1) == false);
assert(static_cast<bool>(opt2) == true);
assert(*opt2 == 2);
X::dtor_called = 0;
opt1.swap(opt2);
assert(X::dtor_called == 1);
assert(static_cast<bool>(opt1) == true);
assert(*opt1 == 2);
assert(static_cast<bool>(opt2) == false);
}
{
std::optional<X> opt1(1);
std::optional<X> opt2(2);
static_assert(noexcept(opt1.swap(opt2)) == true, "");
assert(static_cast<bool>(opt1) == true);
assert(*opt1 == 1);
assert(static_cast<bool>(opt2) == true);
assert(*opt2 == 2);
X::dtor_called = 0;
opt1.swap(opt2);
assert(X::dtor_called == 1); // from inside std::swap
assert(static_cast<bool>(opt1) == true);
assert(*opt1 == 2);
assert(static_cast<bool>(opt2) == true);
assert(*opt2 == 1);
}
{
std::optional<Y> opt1;
std::optional<Y> opt2;
static_assert(noexcept(opt1.swap(opt2)) == false, "");
assert(static_cast<bool>(opt1) == false);
assert(static_cast<bool>(opt2) == false);
opt1.swap(opt2);
assert(static_cast<bool>(opt1) == false);
assert(static_cast<bool>(opt2) == false);
assert(Y::dtor_called == 0);
}
{
std::optional<Y> opt1(1);
std::optional<Y> opt2;
static_assert(noexcept(opt1.swap(opt2)) == false, "");
assert(static_cast<bool>(opt1) == true);
assert(*opt1 == 1);
assert(static_cast<bool>(opt2) == false);
Y::dtor_called = 0;
opt1.swap(opt2);
assert(Y::dtor_called == 1);
assert(static_cast<bool>(opt1) == false);
assert(static_cast<bool>(opt2) == true);
assert(*opt2 == 1);
}
{
std::optional<Y> opt1;
std::optional<Y> opt2(2);
static_assert(noexcept(opt1.swap(opt2)) == false, "");
assert(static_cast<bool>(opt1) == false);
assert(static_cast<bool>(opt2) == true);
assert(*opt2 == 2);
Y::dtor_called = 0;
opt1.swap(opt2);
assert(Y::dtor_called == 1);
assert(static_cast<bool>(opt1) == true);
assert(*opt1 == 2);
assert(static_cast<bool>(opt2) == false);
}
{
std::optional<Y> opt1(1);
std::optional<Y> opt2(2);
static_assert(noexcept(opt1.swap(opt2)) == false, "");
assert(static_cast<bool>(opt1) == true);
assert(*opt1 == 1);
assert(static_cast<bool>(opt2) == true);
assert(*opt2 == 2);
Y::dtor_called = 0;
opt1.swap(opt2);
assert(Y::dtor_called == 0);
assert(static_cast<bool>(opt1) == true);
assert(*opt1 == 2);
assert(static_cast<bool>(opt2) == true);
assert(*opt2 == 1);
}
{
std::optional<Z> opt1;
std::optional<Z> opt2;
static_assert(noexcept(opt1.swap(opt2)) == false, "");
assert(static_cast<bool>(opt1) == false);
assert(static_cast<bool>(opt2) == false);
opt1.swap(opt2);
assert(static_cast<bool>(opt1) == false);
assert(static_cast<bool>(opt2) == false);
}
{
std::optional<Z> opt1;
opt1.emplace(1);
std::optional<Z> opt2;
static_assert(noexcept(opt1.swap(opt2)) == false, "");
assert(static_cast<bool>(opt1) == true);
assert(*opt1 == 1);
assert(static_cast<bool>(opt2) == false);
try
{
opt1.swap(opt2);
assert(false);
}
catch (int i)
{
assert(i == 7);
}
assert(static_cast<bool>(opt1) == true);
assert(*opt1 == 1);
assert(static_cast<bool>(opt2) == false);
}
{
std::optional<Z> opt1;
std::optional<Z> opt2;
opt2.emplace(2);
static_assert(noexcept(opt1.swap(opt2)) == false, "");
assert(static_cast<bool>(opt1) == false);
assert(static_cast<bool>(opt2) == true);
assert(*opt2 == 2);
try
{
opt1.swap(opt2);
assert(false);
}
catch (int i)
{
assert(i == 7);
}
assert(static_cast<bool>(opt1) == false);
assert(static_cast<bool>(opt2) == true);
assert(*opt2 == 2);
}
{
std::optional<Z> opt1;
opt1.emplace(1);
std::optional<Z> opt2;
opt2.emplace(2);
static_assert(noexcept(opt1.swap(opt2)) == false, "");
assert(static_cast<bool>(opt1) == true);
assert(*opt1 == 1);
assert(static_cast<bool>(opt2) == true);
assert(*opt2 == 2);
try
{
opt1.swap(opt2);
assert(false);
}
catch (int i)
{
assert(i == 6);
}
assert(static_cast<bool>(opt1) == true);
assert(*opt1 == 1);
assert(static_cast<bool>(opt2) == true);
assert(*opt2 == 2);
}
#endif // _LIBCPP_STD_VER > 11
}

View File

@@ -0,0 +1,23 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <optional>
// T shall be an object type and shall satisfy the requirements of Destructible
#include <optional>
int main()
{
#if _LIBCPP_STD_VER > 11
std::optional<const void> opt;
#else
#error
#endif // _LIBCPP_STD_VER > 11
}

View File

@@ -0,0 +1,33 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <optional>
// T shall be an object type and shall satisfy the requirements of Destructible
#include <optional>
#if _LIBCPP_STD_VER > 11
struct X
{
private:
~X() {}
};
#endif // _LIBCPP_STD_VER > 11
int main()
{
#if _LIBCPP_STD_VER > 11
std::optional<X> opt;
#else
#error
#endif // _LIBCPP_STD_VER > 11
}

View File

@@ -0,0 +1,32 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <optional>
// T shall be an object type and shall satisfy the requirements of Destructible
#include <optional>
#if _LIBCPP_STD_VER > 11
struct X
{
~X() noexcept(false) {}
};
#endif // _LIBCPP_STD_VER > 11
int main()
{
#if _LIBCPP_STD_VER > 11
std::optional<X> opt;
#else
#error
#endif // _LIBCPP_STD_VER > 11
}

View File

@@ -0,0 +1,23 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <optional>
// T shall be an object type and shall satisfy the requirements of Destructible
#include <optional>
int main()
{
#if _LIBCPP_STD_VER > 11
std::optional<void> opt;
#else
#error
#endif // _LIBCPP_STD_VER > 11
}

View File

@@ -0,0 +1,41 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <optional>
// template <class T>
// class optional
// {
// public:
// typedef T value_type;
// ...
#include <optional>
#include <type_traits>
#if _LIBCPP_STD_VER > 11
template <class Opt, class T>
void
test()
{
static_assert(std::is_same<typename Opt::value_type, T>::value, "");
}
#endif // _LIBCPP_STD_VER > 11
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>();
#endif // _LIBCPP_STD_VER > 11
}