Initial commit

This commit is contained in:
Yervant7
2024-01-16 19:28:29 -03:00
commit 8c0524dc0f
9054 changed files with 1081727 additions and 0 deletions
@@ -0,0 +1,285 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14
// <optional>
// template <class U> optional<T>& operator=(U&& v);
#include <optional>
#include <type_traits>
#include <cassert>
#include <memory>
#include "test_macros.h"
#include "archetypes.h"
using std::optional;
struct ThrowAssign {
static int dtor_called;
ThrowAssign() = default;
ThrowAssign(int) { TEST_THROW(42); }
ThrowAssign& operator=(int) {
TEST_THROW(42);
}
~ThrowAssign() { ++dtor_called; }
};
int ThrowAssign::dtor_called = 0;
template <class T, class Arg = T, bool Expect = true>
void assert_assignable() {
static_assert(std::is_assignable<optional<T>&, Arg>::value == Expect, "");
static_assert(!std::is_assignable<const optional<T>&, Arg>::value, "");
}
struct MismatchType {
explicit MismatchType(int) {}
explicit MismatchType(char*) {}
explicit MismatchType(int*) = delete;
MismatchType& operator=(int) { return *this; }
MismatchType& operator=(int*) { return *this; }
MismatchType& operator=(char*) = delete;
};
struct FromOptionalType {
using Opt = std::optional<FromOptionalType>;
FromOptionalType() = default;
FromOptionalType(FromOptionalType const&) = delete;
template <class Dummy = void>
constexpr FromOptionalType(Opt&) { Dummy::BARK; }
template <class Dummy = void>
constexpr FromOptionalType& operator=(Opt&) { Dummy::BARK; return *this; }
};
void test_sfinae() {
using I = TestTypes::TestType;
using E = ExplicitTestTypes::TestType;
assert_assignable<int>();
assert_assignable<int, int&>();
assert_assignable<int, int const&>();
// Implicit test type
assert_assignable<I, I const&>();
assert_assignable<I, I&&>();
assert_assignable<I, int>();
assert_assignable<I, void*, false>();
// Explicit test type
assert_assignable<E, E const&>();
assert_assignable<E, E &&>();
assert_assignable<E, int>();
assert_assignable<E, void*, false>();
// Mismatch type
assert_assignable<MismatchType, int>();
assert_assignable<MismatchType, int*, false>();
assert_assignable<MismatchType, char*, false>();
// Type constructible from optional
assert_assignable<FromOptionalType, std::optional<FromOptionalType>&, false>();
}
void test_with_test_type()
{
using T = TestTypes::TestType;
T::reset();
{ // to empty
optional<T> opt;
opt = 3;
assert(T::alive == 1);
assert(T::constructed == 1);
assert(T::value_constructed == 1);
assert(T::assigned == 0);
assert(T::destroyed == 0);
assert(static_cast<bool>(opt) == true);
assert(*opt == T(3));
}
{ // to existing
optional<T> opt(42);
T::reset_constructors();
opt = 3;
assert(T::alive == 1);
assert(T::constructed == 0);
assert(T::assigned == 1);
assert(T::value_assigned == 1);
assert(T::destroyed == 0);
assert(static_cast<bool>(opt) == true);
assert(*opt == T(3));
}
{ // test default argument
optional<T> opt;
T::reset_constructors();
opt = {1, 2};
assert(T::alive == 1);
assert(T::constructed == 2);
assert(T::value_constructed == 1);
assert(T::move_constructed == 1);
assert(T::assigned == 0);
assert(T::destroyed == 1);
assert(static_cast<bool>(opt) == true);
assert(*opt == T(1, 2));
}
{ // test default argument
optional<T> opt(42);
T::reset_constructors();
opt = {1, 2};
assert(T::alive == 1);
assert(T::constructed == 1);
assert(T::value_constructed == 1);
assert(T::assigned == 1);
assert(T::move_assigned == 1);
assert(T::destroyed == 1);
assert(static_cast<bool>(opt) == true);
assert(*opt == T(1, 2));
}
{ // test default argument
optional<T> opt;
T::reset_constructors();
opt = {1};
assert(T::alive == 1);
assert(T::constructed == 2);
assert(T::value_constructed == 1);
assert(T::move_constructed == 1);
assert(T::assigned == 0);
assert(T::destroyed == 1);
assert(static_cast<bool>(opt) == true);
assert(*opt == T(1));
}
{ // test default argument
optional<T> opt(42);
T::reset_constructors();
opt = {};
assert(static_cast<bool>(opt) == false);
assert(T::alive == 0);
assert(T::constructed == 0);
assert(T::assigned == 0);
assert(T::destroyed == 1);
}
}
template <class T, class Value = int>
void test_with_type() {
{ // to empty
optional<T> opt;
opt = Value(3);
assert(static_cast<bool>(opt) == true);
assert(*opt == T(3));
}
{ // to existing
optional<T> opt(Value(42));
opt = Value(3);
assert(static_cast<bool>(opt) == true);
assert(*opt == T(3));
}
{ // test const
optional<T> opt(Value(42));
const T t(Value(3));
opt = t;
assert(static_cast<bool>(opt) == true);
assert(*opt == T(3));
}
{ // test default argument
optional<T> opt;
opt = {Value(1)};
assert(static_cast<bool>(opt) == true);
assert(*opt == T(1));
}
{ // test default argument
optional<T> opt(Value(42));
opt = {};
assert(static_cast<bool>(opt) == false);
}
}
template <class T>
void test_with_type_multi() {
test_with_type<T>();
{ // test default argument
optional<T> opt;
opt = {1, 2};
assert(static_cast<bool>(opt) == true);
assert(*opt == T(1, 2));
}
{ // test default argument
optional<T> opt(42);
opt = {1, 2};
assert(static_cast<bool>(opt) == true);
assert(*opt == T(1, 2));
}
}
void test_throws()
{
#ifndef TEST_HAS_NO_EXCEPTIONS
using T = ThrowAssign;
{
optional<T> opt;
try {
opt = 42;
assert(false);
} catch (int) {}
assert(static_cast<bool>(opt) == false);
}
assert(T::dtor_called == 0);
{
T::dtor_called = 0;
optional<T> opt(std::in_place);
try {
opt = 42;
assert(false);
} catch (int) {}
assert(static_cast<bool>(opt) == true);
assert(T::dtor_called == 0);
}
assert(T::dtor_called == 1);
#endif
}
enum MyEnum { Zero, One, Two, Three, FortyTwo = 42 };
using Fn = void(*)();
// https://llvm.org/PR38638
template <class T>
constexpr T pr38638(T v)
{
std::optional<T> o;
o = v;
return *o + 2;
}
int main(int, char**)
{
test_sfinae();
// Test with instrumented type
test_with_test_type();
// Test with various scalar types
test_with_type<int>();
test_with_type<MyEnum, MyEnum>();
test_with_type<int, MyEnum>();
test_with_type<Fn, Fn>();
// Test types with multi argument constructors
test_with_type_multi<ConstexprTestTypes::TestType>();
test_with_type_multi<TrivialTestTypes::TestType>();
// Test move only types
{
optional<std::unique_ptr<int>> opt;
opt = std::unique_ptr<int>(new int(3));
assert(static_cast<bool>(opt) == true);
assert(**opt == 3);
}
{
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);
}
test_throws();
static_assert(pr38638(3) == 5, "");
return 0;
}
@@ -0,0 +1,255 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14
// <optional>
// From LWG2451:
// template<class U>
// optional<T>& operator=(const optional<U>& rhs);
#include <optional>
#include <type_traits>
#include <cassert>
#include "test_macros.h"
#include "archetypes.h"
using std::optional;
struct X
{
static bool throw_now;
X() = default;
X(int)
{
if (throw_now)
TEST_THROW(6);
}
};
bool X::throw_now = false;
struct Y1
{
Y1() = default;
Y1(const int&) {}
Y1& operator=(const Y1&) = delete;
};
struct Y2
{
Y2() = default;
Y2(const int&) = delete;
Y2& operator=(const int&) { return *this; }
};
template <class T>
struct AssignableFrom {
static int type_constructed;
static int type_assigned;
static int int_constructed;
static int int_assigned;
static void reset() {
type_constructed = int_constructed = 0;
type_assigned = int_assigned = 0;
}
AssignableFrom() = default;
explicit AssignableFrom(T) { ++type_constructed; }
AssignableFrom& operator=(T) { ++type_assigned; return *this; }
AssignableFrom(int) { ++int_constructed; }
AssignableFrom& operator=(int) { ++int_assigned; return *this; }
private:
AssignableFrom(AssignableFrom const&) = delete;
AssignableFrom& operator=(AssignableFrom const&) = delete;
};
template <class T> int AssignableFrom<T>::type_constructed = 0;
template <class T> int AssignableFrom<T>::type_assigned = 0;
template <class T> int AssignableFrom<T>::int_constructed = 0;
template <class T> int AssignableFrom<T>::int_assigned = 0;
void test_with_test_type() {
using T = TestTypes::TestType;
T::reset();
{ // non-empty to empty
T::reset_constructors();
optional<T> opt;
const optional<int> other(42);
opt = other;
assert(T::alive == 1);
assert(T::constructed == 1);
assert(T::value_constructed == 1);
assert(T::assigned == 0);
assert(T::destroyed == 0);
assert(static_cast<bool>(other) == true);
assert(*other == 42);
assert(static_cast<bool>(opt) == true);
assert(*opt == T(42));
}
assert(T::alive == 0);
{ // non-empty to non-empty
optional<T> opt(101);
const optional<int> other(42);
T::reset_constructors();
opt = other;
assert(T::alive == 1);
assert(T::constructed == 0);
assert(T::assigned == 1);
assert(T::value_assigned == 1);
assert(T::destroyed == 0);
assert(static_cast<bool>(other) == true);
assert(*other == 42);
assert(static_cast<bool>(opt) == true);
assert(*opt == T(42));
}
assert(T::alive == 0);
{ // empty to non-empty
optional<T> opt(101);
const optional<int> other;
T::reset_constructors();
opt = other;
assert(T::alive == 0);
assert(T::constructed == 0);
assert(T::assigned == 0);
assert(T::destroyed == 1);
assert(static_cast<bool>(other) == false);
assert(static_cast<bool>(opt) == false);
}
assert(T::alive == 0);
{ // empty to empty
optional<T> opt;
const optional<int> other;
T::reset_constructors();
opt = other;
assert(T::alive == 0);
assert(T::constructed == 0);
assert(T::assigned == 0);
assert(T::destroyed == 0);
assert(static_cast<bool>(other) == false);
assert(static_cast<bool>(opt) == false);
}
assert(T::alive == 0);
}
void test_ambiguous_assign() {
using OptInt = std::optional<int>;
{
using T = AssignableFrom<OptInt const&>;
const OptInt a(42);
T::reset();
{
std::optional<T> t;
t = a;
assert(T::type_constructed == 1);
assert(T::type_assigned == 0);
assert(T::int_constructed == 0);
assert(T::int_assigned == 0);
}
T::reset();
{
std::optional<T> t(42);
t = a;
assert(T::type_constructed == 0);
assert(T::type_assigned == 1);
assert(T::int_constructed == 1);
assert(T::int_assigned == 0);
}
T::reset();
{
std::optional<T> t(42);
t = std::move(a);
assert(T::type_constructed == 0);
assert(T::type_assigned == 1);
assert(T::int_constructed == 1);
assert(T::int_assigned == 0);
}
}
{
using T = AssignableFrom<OptInt&>;
OptInt a(42);
T::reset();
{
std::optional<T> t;
t = a;
assert(T::type_constructed == 1);
assert(T::type_assigned == 0);
assert(T::int_constructed == 0);
assert(T::int_assigned == 0);
}
{
using Opt = std::optional<T>;
static_assert(!std::is_assignable_v<Opt&, OptInt const&>, "");
}
}
}
int main(int, char**)
{
test_with_test_type();
test_ambiguous_assign();
{
optional<int> opt;
constexpr optional<short> opt2;
opt = opt2;
static_assert(static_cast<bool>(opt2) == false, "");
assert(static_cast<bool>(opt) == static_cast<bool>(opt2));
}
{
optional<int> opt;
constexpr optional<short> opt2(short{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);
}
{
optional<int> opt(3);
constexpr optional<short> opt2;
opt = opt2;
static_assert(static_cast<bool>(opt2) == false, "");
assert(static_cast<bool>(opt) == static_cast<bool>(opt2));
}
{
optional<int> opt(3);
constexpr optional<short> opt2(short{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);
}
#ifndef TEST_HAS_NO_EXCEPTIONS
{
optional<X> opt;
optional<int> opt2(42);
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
return 0;
}
@@ -0,0 +1,108 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14
// <optional>
// optional<T>& operator=(const optional<T>& rhs); // constexpr in C++20
#include <optional>
#include <type_traits>
#include <cassert>
#include "test_macros.h"
#include "archetypes.h"
using std::optional;
struct X
{
static bool throw_now;
X() = default;
X(const X&)
{
if (throw_now)
TEST_THROW(6);
}
X& operator=(X const&) = default;
};
bool X::throw_now = false;
template <class Tp>
constexpr bool assign_empty(optional<Tp>&& lhs) {
const optional<Tp> rhs;
lhs = rhs;
return !lhs.has_value() && !rhs.has_value();
}
template <class Tp>
constexpr bool assign_value(optional<Tp>&& lhs) {
const optional<Tp> rhs(101);
lhs = rhs;
return lhs.has_value() && rhs.has_value() && *lhs == *rhs;
}
int main(int, char**)
{
{
using O = optional<int>;
#if TEST_STD_VER > 17
LIBCPP_STATIC_ASSERT(assign_empty(O{42}), "");
LIBCPP_STATIC_ASSERT(assign_value(O{42}), "");
#endif
assert(assign_empty(O{42}));
assert(assign_value(O{42}));
}
{
using O = optional<TrivialTestTypes::TestType>;
#if TEST_STD_VER > 17
LIBCPP_STATIC_ASSERT(assign_empty(O{42}), "");
LIBCPP_STATIC_ASSERT(assign_value(O{42}), "");
#endif
assert(assign_empty(O{42}));
assert(assign_value(O{42}));
}
{
using O = optional<TestTypes::TestType>;
assert(assign_empty(O{42}));
assert(assign_value(O{42}));
}
{
using T = TestTypes::TestType;
T::reset();
optional<T> opt(3);
const optional<T> opt2;
assert(T::alive == 1);
opt = opt2;
assert(T::alive == 0);
assert(!opt2.has_value());
assert(!opt.has_value());
}
#ifndef TEST_HAS_NO_EXCEPTIONS
{
optional<X> opt;
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
return 0;
}
@@ -0,0 +1,295 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14
// <optional>
// template <class... Args> T& optional<T>::emplace(Args&&... args);
#include <optional>
#include <type_traits>
#include <cassert>
#include <memory>
#include "test_macros.h"
#include "archetypes.h"
using std::optional;
class X
{
int i_;
int j_ = 0;
public:
constexpr X() : i_(0) {}
constexpr X(int i) : i_(i) {}
constexpr X(int i, int j) : i_(i), j_(j) {}
friend constexpr 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(int) { TEST_THROW(6);}
~Y() {dtor_called = true;}
};
bool Y::dtor_called = false;
template <class T>
constexpr bool test_one_arg() {
using Opt = std::optional<T>;
{
Opt opt;
auto & v = opt.emplace();
static_assert( std::is_same_v<T&, decltype(v)>, "" );
assert(static_cast<bool>(opt) == true);
assert(*opt == T(0));
assert(&v == &*opt);
}
{
Opt opt;
auto & v = opt.emplace(1);
static_assert( std::is_same_v<T&, decltype(v)>, "" );
assert(static_cast<bool>(opt) == true);
assert(*opt == T(1));
assert(&v == &*opt);
}
{
Opt opt(2);
auto & v = opt.emplace();
static_assert( std::is_same_v<T&, decltype(v)>, "" );
assert(static_cast<bool>(opt) == true);
assert(*opt == T(0));
assert(&v == &*opt);
}
{
Opt opt(2);
auto & v = opt.emplace(1);
static_assert( std::is_same_v<T&, decltype(v)>, "" );
assert(static_cast<bool>(opt) == true);
assert(*opt == T(1));
assert(&v == &*opt);
}
return true;
}
template <class T>
constexpr bool test_multi_arg()
{
test_one_arg<T>();
using Opt = std::optional<T>;
{
Opt opt;
auto &v = opt.emplace(101, 41);
static_assert( std::is_same_v<T&, decltype(v)>, "" );
assert(static_cast<bool>(opt) == true);
assert( v == T(101, 41));
assert(*opt == T(101, 41));
}
{
Opt opt;
auto &v = opt.emplace({1, 2, 3, 4});
static_assert( std::is_same_v<T&, decltype(v)>, "" );
assert(static_cast<bool>(opt) == true);
assert( v == T(4)); // T sets its value to the size of the init list
assert(*opt == T(4));
}
{
Opt opt;
auto &v = opt.emplace({1, 2, 3, 4, 5}, 6);
static_assert( std::is_same_v<T&, decltype(v)>, "" );
assert(static_cast<bool>(opt) == true);
assert( v == T(5)); // T sets its value to the size of the init list
assert(*opt == T(5)); // T sets its value to the size of the init list
}
return true;
}
template <class T>
void test_on_test_type() {
T::reset();
optional<T> opt;
assert(T::alive == 0);
{
T::reset_constructors();
auto &v = opt.emplace();
static_assert( std::is_same_v<T&, decltype(v)>, "" );
assert(T::alive == 1);
assert(T::constructed == 1);
assert(T::default_constructed == 1);
assert(T::destroyed == 0);
assert(static_cast<bool>(opt) == true);
assert(*opt == T());
assert(&v == &*opt);
}
{
T::reset_constructors();
auto &v = opt.emplace();
static_assert( std::is_same_v<T&, decltype(v)>, "" );
assert(T::alive == 1);
assert(T::constructed == 1);
assert(T::default_constructed == 1);
assert(T::destroyed == 1);
assert(static_cast<bool>(opt) == true);
assert(*opt == T());
assert(&v == &*opt);
}
{
T::reset_constructors();
auto &v = opt.emplace(101);
static_assert( std::is_same_v<T&, decltype(v)>, "" );
assert(T::alive == 1);
assert(T::constructed == 1);
assert(T::value_constructed == 1);
assert(T::destroyed == 1);
assert(static_cast<bool>(opt) == true);
assert(*opt == T(101));
assert(&v == &*opt);
}
{
T::reset_constructors();
auto &v = opt.emplace(-10, 99);
static_assert( std::is_same_v<T&, decltype(v)>, "" );
assert(T::alive == 1);
assert(T::constructed == 1);
assert(T::value_constructed == 1);
assert(T::destroyed == 1);
assert(static_cast<bool>(opt) == true);
assert(*opt == T(-10, 99));
assert(&v == &*opt);
}
{
T::reset_constructors();
auto &v = opt.emplace(-10, 99);
static_assert( std::is_same_v<T&, decltype(v)>, "" );
assert(T::alive == 1);
assert(T::constructed == 1);
assert(T::value_constructed == 1);
assert(T::destroyed == 1);
assert(static_cast<bool>(opt) == true);
assert(*opt == T(-10, 99));
assert(&v == &*opt);
}
{
T::reset_constructors();
auto &v = opt.emplace({-10, 99, 42, 1});
static_assert( std::is_same_v<T&, decltype(v)>, "" );
assert(T::alive == 1);
assert(T::constructed == 1);
assert(T::value_constructed == 1);
assert(T::destroyed == 1);
assert(static_cast<bool>(opt) == true);
assert(*opt == T(4)); // size of the initializer list
assert(&v == &*opt);
}
{
T::reset_constructors();
auto &v = opt.emplace({-10, 99, 42, 1}, 42);
static_assert( std::is_same_v<T&, decltype(v)>, "" );
assert(T::alive == 1);
assert(T::constructed == 1);
assert(T::value_constructed == 1);
assert(T::destroyed == 1);
assert(static_cast<bool>(opt) == true);
assert(*opt == T(4)); // size of the initializer list
assert(&v == &*opt);
}
}
constexpr bool test_empty_emplace()
{
optional<const int> opt;
auto &v = opt.emplace(42);
static_assert( std::is_same_v<const int&, decltype(v)>, "" );
assert(*opt == 42);
assert( v == 42);
opt.emplace();
assert(*opt == 0);
return true;
}
int main(int, char**)
{
{
test_on_test_type<TestTypes::TestType>();
test_on_test_type<ExplicitTestTypes::TestType>();
}
{
using T = int;
test_one_arg<T>();
test_one_arg<const T>();
#if TEST_STD_VER > 17
static_assert(test_one_arg<T>());
static_assert(test_one_arg<const T>());
#endif
}
{
using T = ConstexprTestTypes::TestType;
test_multi_arg<T>();
#if TEST_STD_VER > 17
static_assert(test_multi_arg<T>());
#endif
}
{
using T = ExplicitConstexprTestTypes::TestType;
test_multi_arg<T>();
#if TEST_STD_VER > 17
static_assert(test_multi_arg<T>());
#endif
}
{
using T = TrivialTestTypes::TestType;
test_multi_arg<T>();
#if TEST_STD_VER > 17
static_assert(test_multi_arg<T>());
#endif
}
{
using T = ExplicitTrivialTestTypes::TestType;
test_multi_arg<T>();
#if TEST_STD_VER > 17
static_assert(test_multi_arg<T>());
#endif
}
{
test_empty_emplace();
#if TEST_STD_VER > 17
static_assert(test_empty_emplace());
#endif
}
#ifndef TEST_HAS_NO_EXCEPTIONS
Y::dtor_called = false;
{
Y y;
optional<Y> opt(y);
try
{
assert(static_cast<bool>(opt) == true);
assert(Y::dtor_called == false);
auto &v = opt.emplace(1);
static_assert( std::is_same_v<Y&, decltype(v)>, "" );
assert(false);
}
catch (int i)
{
assert(i == 6);
assert(static_cast<bool>(opt) == false);
assert(Y::dtor_called == true);
}
}
#endif
return 0;
}
@@ -0,0 +1,140 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14
// <optional>
// template <class U, class... Args>
// T& optional<T>::emplace(initializer_list<U> il, Args&&... args);
#include <optional>
#include <type_traits>
#include <cassert>
#include <vector>
#include "test_macros.h"
using std::optional;
class X
{
int i_;
int j_ = 0;
bool* dtor_called_;
public:
constexpr X(bool& dtor_called) : i_(0), dtor_called_(&dtor_called) {}
constexpr X(int i, bool& dtor_called) : i_(i), dtor_called_(&dtor_called) {}
constexpr X(std::initializer_list<int> il, bool& dtor_called)
: i_(il.begin()[0]), j_(il.begin()[1]), dtor_called_(&dtor_called) {}
TEST_CONSTEXPR_CXX20 ~X() {*dtor_called_ = true;}
friend constexpr 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:
static bool dtor_called;
Z() : i_(0) {}
Z(int i) : i_(i) {}
Z(std::initializer_list<int> il) : i_(il.begin()[0]), j_(il.begin()[1])
{ TEST_THROW(6);}
~Z() {dtor_called = true;}
friend bool operator==(const Z& x, const Z& y)
{return x.i_ == y.i_ && x.j_ == y.j_;}
};
bool Z::dtor_called = false;
TEST_CONSTEXPR_CXX20 bool check_X()
{
bool dtor_called = false;
X x(dtor_called);
optional<X> opt(x);
assert(dtor_called == false);
auto &v = opt.emplace({1, 2}, dtor_called);
static_assert( std::is_same_v<X&, decltype(v)>, "" );
assert(dtor_called);
assert(*opt == X({1, 2}, dtor_called));
assert(&v == &*opt);
return true;
}
TEST_CONSTEXPR_CXX20 bool check_Y()
{
optional<Y> opt;
auto &v = opt.emplace({1, 2});
static_assert( std::is_same_v<Y&, decltype(v)>, "" );
assert(static_cast<bool>(opt) == true);
assert(*opt == Y({1, 2}));
assert(&v == &*opt);
return true;
}
int main(int, char**)
{
{
check_X();
#if TEST_STD_VER > 17
static_assert(check_X());
#endif
}
{
optional<std::vector<int>> opt;
auto &v = opt.emplace({1, 2, 3}, std::allocator<int>());
static_assert( std::is_same_v<std::vector<int>&, decltype(v)>, "" );
assert(static_cast<bool>(opt) == true);
assert(*opt == std::vector<int>({1, 2, 3}));
assert(&v == &*opt);
}
{
check_Y();
#if TEST_STD_VER > 17
static_assert(check_Y());
#endif
}
#ifndef TEST_HAS_NO_EXCEPTIONS
{
Z z;
optional<Z> opt(z);
try
{
assert(static_cast<bool>(opt) == true);
assert(Z::dtor_called == false);
auto &v = opt.emplace({1, 2});
static_assert( std::is_same_v<Z&, decltype(v)>, "" );
assert(false);
}
catch (int i)
{
assert(i == 6);
assert(static_cast<bool>(opt) == false);
assert(Z::dtor_called == true);
}
}
#endif
return 0;
}
@@ -0,0 +1,208 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14
// <optional>
// optional<T>& operator=(optional<T>&& rhs)
// noexcept(is_nothrow_move_assignable<T>::value &&
// is_nothrow_move_constructible<T>::value); // constexpr in C++20
#include <optional>
#include <cassert>
#include <type_traits>
#include <utility>
#include "test_macros.h"
#include "archetypes.h"
using std::optional;
struct X
{
static bool throw_now;
static int alive;
X() { ++alive; }
X(X&&)
{
if (throw_now)
TEST_THROW(6);
++alive;
}
X& operator=(X&&)
{
if (throw_now)
TEST_THROW(42);
return *this;
}
~X() { assert(alive > 0); --alive; }
};
struct Y {};
bool X::throw_now = false;
int X::alive = 0;
template <class Tp>
constexpr bool assign_empty(optional<Tp>&& lhs) {
optional<Tp> rhs;
lhs = std::move(rhs);
return !lhs.has_value() && !rhs.has_value();
}
template <class Tp>
constexpr bool assign_value(optional<Tp>&& lhs) {
optional<Tp> rhs(101);
lhs = std::move(rhs);
return lhs.has_value() && rhs.has_value() && *lhs == Tp{101};
}
int main(int, char**)
{
{
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));
}
{
optional<int> opt;
constexpr 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);
}
{
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));
}
{
using T = TestTypes::TestType;
T::reset();
optional<T> opt(3);
optional<T> opt2;
assert(T::alive == 1);
opt = std::move(opt2);
assert(T::alive == 0);
assert(static_cast<bool>(opt2) == false);
assert(static_cast<bool>(opt) == static_cast<bool>(opt2));
}
{
optional<int> opt(3);
constexpr 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);
}
{
using O = optional<int>;
#if TEST_STD_VER > 17
LIBCPP_STATIC_ASSERT(assign_empty(O{42}), "");
LIBCPP_STATIC_ASSERT(assign_value(O{42}), "");
#endif
assert(assign_empty(O{42}));
assert(assign_value(O{42}));
}
{
using O = optional<TrivialTestTypes::TestType>;
#if TEST_STD_VER > 17
LIBCPP_STATIC_ASSERT(assign_empty(O{42}), "");
LIBCPP_STATIC_ASSERT(assign_value(O{42}), "");
#endif
assert(assign_empty(O{42}));
assert(assign_value(O{42}));
}
#ifndef TEST_HAS_NO_EXCEPTIONS
{
static_assert(!std::is_nothrow_move_assignable<optional<X>>::value, "");
X::alive = 0;
X::throw_now = false;
optional<X> opt;
optional<X> opt2(X{});
assert(X::alive == 1);
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);
}
assert(X::alive == 1);
}
assert(X::alive == 0);
{
static_assert(!std::is_nothrow_move_assignable<optional<X>>::value, "");
X::throw_now = false;
optional<X> opt(X{});
optional<X> opt2(X{});
assert(X::alive == 2);
assert(static_cast<bool>(opt2) == true);
try
{
X::throw_now = true;
opt = std::move(opt2);
assert(false);
}
catch (int i)
{
assert(i == 42);
assert(static_cast<bool>(opt) == true);
}
assert(X::alive == 2);
}
assert(X::alive == 0);
#endif // TEST_HAS_NO_EXCEPTIONS
{
static_assert(std::is_nothrow_move_assignable<optional<Y>>::value, "");
}
{
struct ThrowsMove {
ThrowsMove() noexcept {}
ThrowsMove(ThrowsMove const&) noexcept {}
ThrowsMove(ThrowsMove &&) noexcept(false) {}
ThrowsMove& operator=(ThrowsMove const&) noexcept { return *this; }
ThrowsMove& operator=(ThrowsMove &&) noexcept { return *this; }
};
static_assert(!std::is_nothrow_move_assignable<optional<ThrowsMove>>::value, "");
struct ThrowsMoveAssign {
ThrowsMoveAssign() noexcept {}
ThrowsMoveAssign(ThrowsMoveAssign const&) noexcept {}
ThrowsMoveAssign(ThrowsMoveAssign &&) noexcept {}
ThrowsMoveAssign& operator=(ThrowsMoveAssign const&) noexcept { return *this; }
ThrowsMoveAssign& operator=(ThrowsMoveAssign &&) noexcept(false) { return *this; }
};
static_assert(!std::is_nothrow_move_assignable<optional<ThrowsMoveAssign>>::value, "");
struct NoThrowMove {
NoThrowMove() noexcept(false) {}
NoThrowMove(NoThrowMove const&) noexcept(false) {}
NoThrowMove(NoThrowMove &&) noexcept {}
NoThrowMove& operator=(NoThrowMove const&) noexcept { return *this; }
NoThrowMove& operator=(NoThrowMove&&) noexcept { return *this; }
};
static_assert(std::is_nothrow_move_assignable<optional<NoThrowMove>>::value, "");
}
return 0;
}
@@ -0,0 +1,104 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14
// <optional>
// optional<T>& operator=(nullopt_t) noexcept;
#include <optional>
#include <type_traits>
#include <cassert>
#include "test_macros.h"
#include "archetypes.h"
using std::optional;
using std::nullopt_t;
using std::nullopt;
TEST_CONSTEXPR_CXX20 bool test()
{
enum class State { inactive, constructed, destroyed };
State state = State::inactive;
struct StateTracker {
TEST_CONSTEXPR_CXX20 StateTracker(State& s)
: state_(&s)
{
s = State::constructed;
}
TEST_CONSTEXPR_CXX20 ~StateTracker() { *state_ = State::destroyed; }
State* state_;
};
{
optional<int> opt;
static_assert(noexcept(opt = nullopt) == true, "");
opt = nullopt;
assert(static_cast<bool>(opt) == false);
}
{
optional<int> opt(3);
opt = nullopt;
assert(static_cast<bool>(opt) == false);
}
{
optional<StateTracker> opt;
opt = nullopt;
assert(state == State::inactive);
assert(static_cast<bool>(opt) == false);
}
{
optional<StateTracker> opt(state);
assert(state == State::constructed);
opt = nullopt;
assert(state == State::destroyed);
assert(static_cast<bool>(opt) == false);
}
return true;
}
int main(int, char**)
{
#if TEST_STD_VER > 17
static_assert(test());
#endif
test();
using TT = TestTypes::TestType;
TT::reset();
{
optional<TT> opt;
static_assert(noexcept(opt = nullopt) == true, "");
assert(TT::destroyed == 0);
opt = nullopt;
assert(TT::constructed == 0);
assert(TT::alive == 0);
assert(TT::destroyed == 0);
assert(static_cast<bool>(opt) == false);
}
assert(TT::alive == 0);
assert(TT::destroyed == 0);
TT::reset();
{
optional<TT> opt(42);
assert(TT::destroyed == 0);
TT::reset_constructors();
opt = nullopt;
assert(TT::constructed == 0);
assert(TT::alive == 0);
assert(TT::destroyed == 1);
assert(static_cast<bool>(opt) == false);
}
assert(TT::alive == 0);
assert(TT::destroyed == 1);
TT::reset();
return 0;
}
@@ -0,0 +1,338 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14
// <optional>
// From LWG2451:
// template <class U>
// optional<T>& operator=(optional<U>&& rhs);
#include <optional>
#include <array>
#include <cassert>
#include <memory>
#include <type_traits>
#include "test_macros.h"
#include "archetypes.h"
using std::optional;
struct X
{
static bool throw_now;
X() = default;
X(int &&)
{
if (throw_now)
TEST_THROW(6);
}
};
bool X::throw_now = false;
struct Y1
{
Y1() = default;
Y1(const int&) {}
Y1& operator=(const Y1&) = delete;
};
struct Y2
{
Y2() = default;
Y2(const int&) = delete;
Y2& operator=(const int&) { return *this; }
};
struct B { virtual ~B() = default; };
class D : public B {};
template <class T>
struct AssignableFrom {
static int type_constructed;
static int type_assigned;
static int int_constructed;
static int int_assigned;
static void reset() {
type_constructed = int_constructed = 0;
type_assigned = int_assigned = 0;
}
AssignableFrom() = default;
explicit AssignableFrom(T) { ++type_constructed; }
AssignableFrom& operator=(T) { ++type_assigned; return *this; }
AssignableFrom(int) { ++int_constructed; }
AssignableFrom& operator=(int) { ++int_assigned; return *this; }
private:
AssignableFrom(AssignableFrom const&) = delete;
AssignableFrom& operator=(AssignableFrom const&) = delete;
};
template <class T> int AssignableFrom<T>::type_constructed = 0;
template <class T> int AssignableFrom<T>::type_assigned = 0;
template <class T> int AssignableFrom<T>::int_constructed = 0;
template <class T> int AssignableFrom<T>::int_assigned = 0;
void test_with_test_type() {
using T = TestTypes::TestType;
T::reset();
{ // non-empty to empty
T::reset_constructors();
optional<T> opt;
optional<int> other(42);
opt = std::move(other);
assert(T::alive == 1);
assert(T::constructed == 1);
assert(T::value_constructed == 1);
assert(T::assigned == 0);
assert(T::destroyed == 0);
assert(static_cast<bool>(other) == true);
assert(*other == 42);
assert(static_cast<bool>(opt) == true);
assert(*opt == T(42));
}
assert(T::alive == 0);
{ // non-empty to non-empty
optional<T> opt(101);
optional<int> other(42);
T::reset_constructors();
opt = std::move(other);
assert(T::alive == 1);
assert(T::constructed == 0);
assert(T::assigned == 1);
assert(T::value_assigned == 1);
assert(T::destroyed == 0);
assert(static_cast<bool>(other) == true);
assert(*other == 42);
assert(static_cast<bool>(opt) == true);
assert(*opt == T(42));
}
assert(T::alive == 0);
{ // empty to non-empty
optional<T> opt(101);
optional<int> other;
T::reset_constructors();
opt = std::move(other);
assert(T::alive == 0);
assert(T::constructed == 0);
assert(T::assigned == 0);
assert(T::destroyed == 1);
assert(static_cast<bool>(other) == false);
assert(static_cast<bool>(opt) == false);
}
assert(T::alive == 0);
{ // empty to empty
optional<T> opt;
optional<int> other;
T::reset_constructors();
opt = std::move(other);
assert(T::alive == 0);
assert(T::constructed == 0);
assert(T::assigned == 0);
assert(T::destroyed == 0);
assert(static_cast<bool>(other) == false);
assert(static_cast<bool>(opt) == false);
}
assert(T::alive == 0);
}
void test_ambiguous_assign() {
using OptInt = std::optional<int>;
{
using T = AssignableFrom<OptInt&&>;
T::reset();
{
OptInt a(42);
std::optional<T> t;
t = std::move(a);
assert(T::type_constructed == 1);
assert(T::type_assigned == 0);
assert(T::int_constructed == 0);
assert(T::int_assigned == 0);
}
{
using Opt = std::optional<T>;
static_assert(!std::is_assignable<Opt&, const OptInt&&>::value, "");
static_assert(!std::is_assignable<Opt&, const OptInt&>::value, "");
static_assert(!std::is_assignable<Opt&, OptInt&>::value, "");
}
}
{
using T = AssignableFrom<OptInt const&&>;
T::reset();
{
const OptInt a(42);
std::optional<T> t;
t = std::move(a);
assert(T::type_constructed == 1);
assert(T::type_assigned == 0);
assert(T::int_constructed == 0);
assert(T::int_assigned == 0);
}
T::reset();
{
OptInt a(42);
std::optional<T> t;
t = std::move(a);
assert(T::type_constructed == 1);
assert(T::type_assigned == 0);
assert(T::int_constructed == 0);
assert(T::int_assigned == 0);
}
{
using Opt = std::optional<T>;
static_assert(std::is_assignable<Opt&, OptInt&&>::value, "");
static_assert(!std::is_assignable<Opt&, const OptInt&>::value, "");
static_assert(!std::is_assignable<Opt&, OptInt&>::value, "");
}
}
}
TEST_CONSTEXPR_CXX20 bool test()
{
{
optional<int> opt;
optional<short> opt2;
opt = std::move(opt2);
assert(static_cast<bool>(opt2) == false);
assert(static_cast<bool>(opt) == static_cast<bool>(opt2));
}
{
optional<int> opt;
optional<short> opt2(short{2});
opt = std::move(opt2);
assert(static_cast<bool>(opt2) == true);
assert(*opt2 == 2);
assert(static_cast<bool>(opt) == static_cast<bool>(opt2));
assert(*opt == *opt2);
}
{
optional<int> opt(3);
optional<short> opt2;
opt = std::move(opt2);
assert(static_cast<bool>(opt2) == false);
assert(static_cast<bool>(opt) == static_cast<bool>(opt2));
}
{
optional<int> opt(3);
optional<short> opt2(short{2});
opt = std::move(opt2);
assert(static_cast<bool>(opt2) == true);
assert(*opt2 == 2);
assert(static_cast<bool>(opt) == static_cast<bool>(opt2));
assert(*opt == *opt2);
}
enum class state_t { inactive, constructed, copy_assigned, move_assigned };
class StateTracker {
public:
constexpr StateTracker(state_t& s)
: state_(&s)
{
*state_ = state_t::constructed;
}
StateTracker(StateTracker&&) = default;
StateTracker(StateTracker const&) = default;
constexpr StateTracker& operator=(StateTracker&& other) noexcept
{
*state_ = state_t::inactive;
state_ = other.state_;
*state_ = state_t::move_assigned;
other.state_ = nullptr;
return *this;
}
constexpr StateTracker& operator=(StateTracker const& other) noexcept
{
*state_ = state_t::inactive;
state_ = other.state_;
*state_ = state_t::copy_assigned;
return *this;
}
private:
state_t* state_;
};
{
auto state = std::array{state_t::inactive, state_t::inactive};
auto opt1 = std::optional<StateTracker>(state[0]);
assert(state[0] == state_t::constructed);
auto opt2 = std::optional<StateTracker>(state[1]);
assert(state[1] == state_t::constructed);
opt1 = std::move(opt2);
assert(state[0] == state_t::inactive);
assert(state[1] == state_t::move_assigned);
}
{
auto state = std::array{state_t::inactive, state_t::inactive};
auto opt1 = std::optional<StateTracker>(state[0]);
assert(state[0] == state_t::constructed);
auto opt2 = std::optional<StateTracker>(state[1]);
assert(state[1] == state_t::constructed);
opt1 = opt2;
assert(state[0] == state_t::inactive);
assert(state[1] == state_t::copy_assigned);
}
return true;
}
int main(int, char**)
{
#if TEST_STD_VER > 17
static_assert(test());
#endif
test_with_test_type();
test_ambiguous_assign();
test();
{
optional<std::unique_ptr<B>> opt;
optional<std::unique_ptr<D>> other(new D());
opt = std::move(other);
assert(static_cast<bool>(opt) == true);
assert(static_cast<bool>(other) == true);
assert(opt->get() != nullptr);
assert(other->get() == nullptr);
}
#ifndef TEST_HAS_NO_EXCEPTIONS
{
optional<X> opt;
optional<int> opt2(42);
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);
}
}
#endif
return 0;
}
@@ -0,0 +1,156 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// UNSUPPORTED: c++03, c++11, c++14
// Throwing bad_optional_access is supported starting in macosx10.13
// XFAIL: use_system_cxx_lib && target={{.+}}-apple-macosx10.{{9|10|11|12}} && !no-exceptions
// <optional>
// template <class U>
// constexpr EXPLICIT optional(U&& u);
#include <optional>
#include <type_traits>
#include <cassert>
#include "test_macros.h"
#include "archetypes.h"
#include "test_convertible.h"
using std::optional;
struct ImplicitThrow
{
constexpr ImplicitThrow(int x) { if (x != -1) TEST_THROW(6);}
};
struct ExplicitThrow
{
constexpr explicit ExplicitThrow(int x) { if (x != -1) TEST_THROW(6);}
};
struct ImplicitAny {
template <class U>
constexpr ImplicitAny(U&&) {}
};
template <class To, class From>
constexpr bool implicit_conversion(optional<To>&& opt, const From& v)
{
using O = optional<To>;
static_assert(test_convertible<O, From>(), "");
static_assert(!test_convertible<O, void*>(), "");
static_assert(!test_convertible<O, From, int>(), "");
return opt && *opt == static_cast<To>(v);
}
template <class To, class Input, class Expect>
constexpr bool explicit_conversion(Input&& in, const Expect& v)
{
using O = optional<To>;
static_assert(std::is_constructible<O, Input>::value, "");
static_assert(!std::is_convertible<Input, O>::value, "");
static_assert(!std::is_constructible<O, void*>::value, "");
static_assert(!std::is_constructible<O, Input, int>::value, "");
optional<To> opt(std::forward<Input>(in));
return opt && *opt == static_cast<To>(v);
}
void test_implicit()
{
{
static_assert(implicit_conversion<long long>(42, 42), "");
}
{
static_assert(implicit_conversion<long double>(3.14, 3.14), "");
}
{
int x = 42;
optional<void* const> o(&x);
assert(*o == &x);
}
{
using T = TrivialTestTypes::TestType;
static_assert(implicit_conversion<T>(42, 42), "");
}
{
using T = TestTypes::TestType;
assert(implicit_conversion<T>(3, T(3)));
}
{
using O = optional<ImplicitAny>;
static_assert(!test_convertible<O, std::in_place_t>(), "");
static_assert(!test_convertible<O, std::in_place_t&>(), "");
static_assert(!test_convertible<O, const std::in_place_t&>(), "");
static_assert(!test_convertible<O, std::in_place_t&&>(), "");
static_assert(!test_convertible<O, const std::in_place_t&&>(), "");
}
#ifndef TEST_HAS_NO_EXCEPTIONS
{
try {
using T = ImplicitThrow;
optional<T> t = 42;
assert(false);
((void)t);
} catch (int) {
}
}
#endif
}
void test_explicit() {
{
using T = ExplicitTrivialTestTypes::TestType;
static_assert(explicit_conversion<T>(42, 42), "");
}
{
using T = ExplicitConstexprTestTypes::TestType;
static_assert(explicit_conversion<T>(42, 42), "");
static_assert(!std::is_convertible<int, T>::value, "");
}
{
using T = ExplicitTestTypes::TestType;
T::reset();
{
assert(explicit_conversion<T>(42, 42));
assert(T::alive == 0);
}
T::reset();
{
optional<T> t(42);
assert(T::alive == 1);
assert(T::value_constructed == 1);
assert(T::move_constructed == 0);
assert(T::copy_constructed == 0);
assert(t.value().value == 42);
}
assert(T::alive == 0);
}
#ifndef TEST_HAS_NO_EXCEPTIONS
{
try {
using T = ExplicitThrow;
optional<T> t(42);
assert(false);
} catch (int) {
}
}
#endif
}
int main(int, char**) {
test_implicit();
test_explicit();
return 0;
}
@@ -0,0 +1,132 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// UNSUPPORTED: c++03, c++11, c++14
// Throwing bad_optional_access is supported starting in macosx10.13
// XFAIL: use_system_cxx_lib && target={{.+}}-apple-macosx10.{{9|10|11|12}} && !no-exceptions
// <optional>
// constexpr optional(const T& v);
#include <optional>
#include <type_traits>
#include <cassert>
#include "test_macros.h"
#include "archetypes.h"
using std::optional;
int main(int, char**)
{
{
typedef int T;
constexpr T t(5);
constexpr optional<T> opt(t);
static_assert(static_cast<bool>(opt) == true, "");
static_assert(*opt == 5, "");
struct test_constexpr_ctor
: public optional<T>
{
constexpr test_constexpr_ctor(const T&) {}
};
}
{
typedef double T;
constexpr T t(3);
constexpr optional<T> opt(t);
static_assert(static_cast<bool>(opt) == true, "");
static_assert(*opt == 3, "");
struct test_constexpr_ctor
: public optional<T>
{
constexpr test_constexpr_ctor(const T&) {}
};
}
{
const int x = 42;
optional<const int> o(x);
assert(*o == x);
}
{
typedef TestTypes::TestType T;
T::reset();
const T t(3);
optional<T> opt = t;
assert(T::alive == 2);
assert(T::copy_constructed == 1);
assert(static_cast<bool>(opt) == true);
assert(opt.value().value == 3);
}
{
typedef ExplicitTestTypes::TestType T;
static_assert(!std::is_convertible<T const&, optional<T>>::value, "");
T::reset();
const T t(3);
optional<T> opt(t);
assert(T::alive == 2);
assert(T::copy_constructed == 1);
assert(static_cast<bool>(opt) == true);
assert(opt.value().value == 3);
}
{
typedef ConstexprTestTypes::TestType T;
constexpr T t(3);
constexpr optional<T> opt = {t};
static_assert(static_cast<bool>(opt) == true, "");
static_assert(opt.value().value == 3, "");
struct test_constexpr_ctor
: public optional<T>
{
constexpr test_constexpr_ctor(const T&) {}
};
}
{
typedef ExplicitConstexprTestTypes::TestType T;
static_assert(!std::is_convertible<const T&, optional<T>>::value, "");
constexpr T t(3);
constexpr optional<T> opt(t);
static_assert(static_cast<bool>(opt) == true, "");
static_assert(opt.value().value == 3, "");
struct test_constexpr_ctor
: public optional<T>
{
constexpr test_constexpr_ctor(const T&) {}
};
}
#ifndef TEST_HAS_NO_EXCEPTIONS
{
struct Z {
Z(int) {}
Z(const Z&) {throw 6;}
};
typedef Z T;
try
{
const T t(3);
optional<T> opt(t);
assert(false);
}
catch (int i)
{
assert(i == 6);
}
}
#endif
return 0;
}
@@ -0,0 +1,120 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14
// <optional>
// template <class U>
// optional(const optional<U>& rhs);
#include <optional>
#include <type_traits>
#include <cassert>
#include "test_macros.h"
using std::optional;
template <class T, class U>
TEST_CONSTEXPR_CXX20 void
test(const optional<U>& rhs, bool is_going_to_throw = false)
{
bool rhs_engaged = static_cast<bool>(rhs);
#ifndef TEST_HAS_NO_EXCEPTIONS
try
{
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);
}
#else
if (is_going_to_throw) return;
optional<T> lhs = rhs;
assert(static_cast<bool>(lhs) == rhs_engaged);
if (rhs_engaged)
assert(*lhs == *rhs);
#endif
}
class X
{
int i_;
public:
constexpr X(int i) : i_(i) {}
constexpr X(const X& x) : i_(x.i_) {}
TEST_CONSTEXPR_CXX20 ~X() {i_ = 0;}
friend constexpr 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_;}
};
int count = 0;
class Z
{
int i_;
public:
Z(int i) : i_(i) {TEST_THROW(6);}
friend bool operator==(const Z& x, const Z& y) {return x.i_ == y.i_;}
};
template<class T, class U>
constexpr bool test_all()
{
{
optional<U> rhs;
test<T>(rhs);
}
{
optional<U> rhs(U{3});
test<T>(rhs);
}
return true;
}
int main(int, char**)
{
test_all<int, short>();
test_all<X, int>();
test_all<Y, int>();
#if TEST_STD_VER > 17
static_assert(test_all<int, short>());
static_assert(test_all<X, int>());
static_assert(test_all<Y, int>());
#endif
{
typedef Z T;
typedef int U;
optional<U> rhs;
test<T>(rhs);
}
{
typedef Z T;
typedef int U;
optional<U> rhs(U{3});
test<T>(rhs, true);
}
static_assert(!(std::is_constructible<optional<X>, const optional<Y>&>::value), "");
return 0;
}
@@ -0,0 +1,174 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14
// <optional>
// constexpr optional(const optional<T>& rhs);
#include <optional>
#include <type_traits>
#include <cassert>
#include "test_macros.h"
#include "archetypes.h"
using std::optional;
template <class T, class ...InitArgs>
void test(InitArgs&&... args)
{
const optional<T> rhs(std::forward<InitArgs>(args)...);
bool rhs_engaged = static_cast<bool>(rhs);
optional<T> lhs = rhs;
assert(static_cast<bool>(lhs) == rhs_engaged);
if (rhs_engaged)
assert(*lhs == *rhs);
}
template <class T, class ...InitArgs>
constexpr bool constexpr_test(InitArgs&&... args)
{
static_assert( std::is_trivially_copy_constructible_v<T>, ""); // requirement
const optional<T> rhs(std::forward<InitArgs>(args)...);
optional<T> lhs = rhs;
return (lhs.has_value() == rhs.has_value()) &&
(lhs.has_value() ? *lhs == *rhs : true);
}
void test_throwing_ctor() {
#ifndef TEST_HAS_NO_EXCEPTIONS
struct Z {
Z() : count(0) {}
Z(Z const& o) : count(o.count + 1)
{ if (count == 2) throw 6; }
int count;
};
const Z z;
const optional<Z> rhs(z);
try
{
optional<Z> lhs(rhs);
assert(false);
}
catch (int i)
{
assert(i == 6);
}
#endif
}
template <class T, class ...InitArgs>
void test_ref(InitArgs&&... args)
{
const optional<T> rhs(std::forward<InitArgs>(args)...);
bool rhs_engaged = static_cast<bool>(rhs);
optional<T> lhs = rhs;
assert(static_cast<bool>(lhs) == rhs_engaged);
if (rhs_engaged)
assert(&(*lhs) == &(*rhs));
}
void test_reference_extension()
{
#if defined(_LIBCPP_VERSION) && 0 // FIXME these extensions are currently disabled.
using T = TestTypes::TestType;
T::reset();
{
T t;
T::reset_constructors();
test_ref<T&>();
test_ref<T&>(t);
assert(T::alive == 1);
assert(T::constructed == 0);
assert(T::assigned == 0);
assert(T::destroyed == 0);
}
assert(T::destroyed == 1);
assert(T::alive == 0);
{
T t;
const T& ct = t;
T::reset_constructors();
test_ref<T const&>();
test_ref<T const&>(t);
test_ref<T const&>(ct);
assert(T::alive == 1);
assert(T::constructed == 0);
assert(T::assigned == 0);
assert(T::destroyed == 0);
}
assert(T::alive == 0);
assert(T::destroyed == 1);
{
static_assert(!std::is_copy_constructible<std::optional<T&&>>::value, "");
static_assert(!std::is_copy_constructible<std::optional<T const&&>>::value, "");
}
#endif
}
int main(int, char**)
{
test<int>();
test<int>(3);
static_assert(constexpr_test<int>(), "" );
static_assert(constexpr_test<int>(3), "" );
{
const optional<const int> o(42);
optional<const int> o2(o);
assert(*o2 == 42);
}
{
using T = TestTypes::TestType;
T::reset();
const optional<T> rhs;
assert(T::alive == 0);
const optional<T> lhs(rhs);
assert(lhs.has_value() == false);
assert(T::alive == 0);
}
TestTypes::TestType::reset();
{
using T = TestTypes::TestType;
T::reset();
const optional<T> rhs(42);
assert(T::alive == 1);
assert(T::value_constructed == 1);
assert(T::copy_constructed == 0);
const optional<T> lhs(rhs);
assert(lhs.has_value());
assert(T::copy_constructed == 1);
assert(T::alive == 2);
}
TestTypes::TestType::reset();
{
using namespace ConstexprTestTypes;
test<TestType>();
test<TestType>(42);
}
{
using namespace TrivialTestTypes;
test<TestType>();
test<TestType>(42);
}
{
test_throwing_ctor();
}
{
test_reference_extension();
}
{
constexpr std::optional<int> o1{4};
constexpr std::optional<int> o2 = o1;
static_assert( *o2 == 4, "" );
}
return 0;
}
@@ -0,0 +1,47 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14
// <optional>
// T shall be an object type other than cv in_place_t or cv nullopt_t
// and shall satisfy the Cpp17Destructible requirements.
// Note: array types do not satisfy the Cpp17Destructible requirements.
#include <optional>
#include <type_traits>
#include <cassert>
#include "test_macros.h"
struct NonDestructible { ~NonDestructible() = delete; };
int main(int, char**)
{
{
std::optional<char &> o1; // expected-error-re@optional:* {{{{(static_assert|static assertion)}} failed{{.*}}instantiation of optional with a reference type is ill-formed}}
std::optional<NonDestructible> o2; // expected-error-re@optional:* {{{{(static_assert|static assertion)}} failed{{.*}}instantiation of optional with a non-destructible type is ill-formed}}
std::optional<char[20]> o3; // expected-error-re@optional:* {{{{(static_assert|static assertion)}} failed{{.*}}instantiation of optional with an array type is ill-formed}}
}
{
std::optional< std::in_place_t> o1; // expected-error-re@optional:* {{{{(static_assert|static assertion)}} failed{{.*}}instantiation of optional with in_place_t is ill-formed}}
std::optional<const std::in_place_t> o2; // expected-error-re@optional:* {{{{(static_assert|static assertion)}} failed{{.*}}instantiation of optional with in_place_t is ill-formed}}
std::optional< volatile std::in_place_t> o3; // expected-error-re@optional:* {{{{(static_assert|static assertion)}} failed{{.*}}instantiation of optional with in_place_t is ill-formed}}
std::optional<const volatile std::in_place_t> o4; // expected-error-re@optional:* {{{{(static_assert|static assertion)}} failed{{.*}}instantiation of optional with in_place_t is ill-formed}}
}
{
std::optional< std::nullopt_t> o1; // expected-error-re@optional:* {{{{(static_assert|static assertion)}} failed{{.*}}instantiation of optional with nullopt_t is ill-formed}}
std::optional<const std::nullopt_t> o2; // expected-error-re@optional:* {{{{(static_assert|static assertion)}} failed{{.*}}instantiation of optional with nullopt_t is ill-formed}}
std::optional< volatile std::nullopt_t> o3; // expected-error-re@optional:* {{{{(static_assert|static assertion)}} failed{{.*}}instantiation of optional with nullopt_t is ill-formed}}
std::optional<const volatile std::nullopt_t> o4; // expected-error-re@optional:* {{{{(static_assert|static assertion)}} failed{{.*}}instantiation of optional with nullopt_t is ill-formed}}
}
return 0;
}
@@ -0,0 +1,37 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14
// <optional>
// template<class T>
// optional(T) -> optional<T>;
#include <optional>
#include <cassert>
struct A {};
int main(int, char**)
{
// Test the explicit deduction guides
// Test the implicit deduction guides
{
// optional()
std::optional opt; // expected-error {{no viable constructor or deduction guide for deduction of template arguments of 'optional'}}
}
{
// optional(nullopt_t)
std::optional opt(std::nullopt); // expected-error-re@optional:* {{{{(static_assert|static assertion)}} failed{{.*}}instantiation of optional with nullopt_t is ill-formed}}
}
return 0;
}
@@ -0,0 +1,78 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// <optional>
// UNSUPPORTED: c++03, c++11, c++14
// template<class T>
// optional(T) -> optional<T>;
#include <optional>
#include <cassert>
#include "test_macros.h"
struct A {};
int main(int, char**)
{
// Test the explicit deduction guides
{
// optional(T)
std::optional opt(5);
ASSERT_SAME_TYPE(decltype(opt), std::optional<int>);
assert(static_cast<bool>(opt));
assert(*opt == 5);
}
{
// optional(T)
std::optional opt(A{});
ASSERT_SAME_TYPE(decltype(opt), std::optional<A>);
assert(static_cast<bool>(opt));
}
{
// optional(const T&);
const int& source = 5;
std::optional opt(source);
ASSERT_SAME_TYPE(decltype(opt), std::optional<int>);
assert(static_cast<bool>(opt));
assert(*opt == 5);
}
{
// optional(T*);
const int* source = nullptr;
std::optional opt(source);
ASSERT_SAME_TYPE(decltype(opt), std::optional<const int*>);
assert(static_cast<bool>(opt));
assert(*opt == nullptr);
}
{
// optional(T[]);
int source[] = {1, 2, 3};
std::optional opt(source);
ASSERT_SAME_TYPE(decltype(opt), std::optional<int*>);
assert(static_cast<bool>(opt));
assert((*opt)[0] == 1);
}
// Test the implicit deduction guides
{
// optional(optional);
std::optional<char> source('A');
std::optional opt(source);
ASSERT_SAME_TYPE(decltype(opt), std::optional<char>);
assert(static_cast<bool>(opt) == static_cast<bool>(source));
assert(*opt == *source);
}
return 0;
}
@@ -0,0 +1,82 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14
// <optional>
// constexpr optional() noexcept;
#include <optional>
#include <type_traits>
#include <cassert>
#include "test_macros.h"
#include "archetypes.h"
using std::optional;
template <class Opt>
void
test_constexpr()
{
static_assert(std::is_nothrow_default_constructible<Opt>::value, "");
static_assert(std::is_trivially_destructible<Opt>::value, "");
static_assert(std::is_trivially_destructible<typename Opt::value_type>::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, "");
static_assert(!std::is_trivially_destructible<Opt>::value, "");
static_assert(!std::is_trivially_destructible<typename Opt::value_type>::value, "");
{
Opt opt;
assert(static_cast<bool>(opt) == false);
}
{
const Opt opt;
assert(static_cast<bool>(opt) == false);
}
struct test_constexpr_ctor
: public Opt
{
constexpr test_constexpr_ctor() {}
};
}
int main(int, char**)
{
test_constexpr<optional<int>>();
test_constexpr<optional<int*>>();
test_constexpr<optional<ImplicitTypes::NoCtors>>();
test_constexpr<optional<NonTrivialTypes::NoCtors>>();
test_constexpr<optional<NonConstexprTypes::NoCtors>>();
test<optional<NonLiteralTypes::NoCtors>>();
// EXTENSIONS
#if defined(_LIBCPP_VERSION) && 0 // FIXME these extensions are currently disabled.
test_constexpr<optional<int&>>();
test_constexpr<optional<const int&>>();
test_constexpr<optional<int&>>();
test_constexpr<optional<NonLiteralTypes::NoCtors&>>();
test_constexpr<optional<NonLiteralTypes::NoCtors&&>>();
#endif
return 0;
}
@@ -0,0 +1,41 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14
// <optional>
// constexpr optional(in_place_t);
// Test that the SFINAE "is_constructible<value_type>" isn't evaluated by the
// in_place_t constructor with no arguments when the Clang is trying to check
// copy constructor.
#include <optional>
#include <type_traits>
#include <cassert>
#include "test_macros.h"
#include "archetypes.h"
using std::optional;
struct Wrapped {
struct Inner {
bool Dummy = true;
};
std::optional<Inner> inner;
};
int main(int, char**) {
static_assert(std::is_default_constructible<Wrapped::Inner>::value, "");
Wrapped w;
w.inner.emplace();
assert(w.inner.has_value());
return 0;
}
@@ -0,0 +1,118 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14
// <optional>
// template <class U>
// explicit optional(const optional<U>& rhs);
#include <optional>
#include <type_traits>
#include <cassert>
#include "test_macros.h"
using std::optional;
template <class T, class U>
TEST_CONSTEXPR_CXX20 void
test(const optional<U>& rhs, bool is_going_to_throw = false)
{
static_assert(!(std::is_convertible<const optional<U>&, optional<T>>::value), "");
bool rhs_engaged = static_cast<bool>(rhs);
#ifndef TEST_HAS_NO_EXCEPTIONS
try
{
optional<T> lhs(rhs);
assert(is_going_to_throw == false);
assert(static_cast<bool>(lhs) == rhs_engaged);
if (rhs_engaged)
assert(*lhs == T(*rhs));
}
catch (int i)
{
assert(i == 6);
}
#else
if (is_going_to_throw) return;
optional<T> lhs(rhs);
assert(static_cast<bool>(lhs) == rhs_engaged);
if (rhs_engaged)
assert(*lhs == T(*rhs));
#endif
}
class X
{
int i_;
public:
constexpr explicit X(int i) : i_(i) {}
constexpr X(const X& x) : i_(x.i_) {}
TEST_CONSTEXPR_CXX20 ~X() {i_ = 0;}
friend constexpr bool operator==(const X& x, const X& y) {return x.i_ == y.i_;}
};
class Y
{
int i_;
public:
constexpr explicit Y(int i) : i_(i) {}
friend constexpr bool operator==(const Y& x, const Y& y) {return x.i_ == y.i_;}
};
int count = 0;
class Z
{
int i_;
public:
explicit Z(int i) : i_(i) {TEST_THROW(6);}
friend bool operator==(const Z& x, const Z& y) {return x.i_ == y.i_;}
};
template<class T, class U>
constexpr bool test_all()
{
{
optional<U> rhs;
test<T>(rhs);
}
{
optional<U> rhs(3);
test<T>(rhs);
}
return true;
}
int main(int, char**)
{
test_all<X, int>();
test_all<Y, int>();
#if TEST_STD_VER > 17
static_assert(test_all<X, int>());
static_assert(test_all<Y, int>());
#endif
{
typedef Z T;
typedef int U;
optional<U> rhs;
test<T>(rhs);
}
{
typedef Z T;
typedef int U;
optional<U> rhs(3);
test<T>(rhs, true);
}
return 0;
}
@@ -0,0 +1,94 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14
// <optional>
// template <class U>
// explicit optional(optional<U>&& rhs);
#include <optional>
#include <type_traits>
#include <cassert>
#include "test_macros.h"
using std::optional;
template <class T, class U>
TEST_CONSTEXPR_CXX20 void test(optional<U>&& rhs, bool is_going_to_throw = false)
{
static_assert(!(std::is_convertible<optional<U>&&, optional<T>>::value), "");
bool rhs_engaged = static_cast<bool>(rhs);
#ifndef TEST_HAS_NO_EXCEPTIONS
try
{
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);
}
#else
if (is_going_to_throw) return;
optional<T> lhs(std::move(rhs));
assert(static_cast<bool>(lhs) == rhs_engaged);
#endif
}
class X
{
int i_;
public:
constexpr explicit X(int i) : i_(i) {}
constexpr X(X&& x) : i_(x.i_) { x.i_ = 0; }
TEST_CONSTEXPR_CXX20 ~X() {i_ = 0;}
friend constexpr bool operator==(const X& x, const X& y) {return x.i_ == y.i_;}
};
int count = 0;
class Z
{
public:
explicit Z(int) { TEST_THROW(6); }
};
TEST_CONSTEXPR_CXX20 bool test()
{
{
optional<int> rhs;
test<X>(std::move(rhs));
}
{
optional<int> rhs(3);
test<X>(std::move(rhs));
}
return true;
}
int main(int, char**)
{
#if TEST_STD_VER > 17
static_assert(test());
#endif
test();
{
optional<int> rhs;
test<Z>(std::move(rhs));
}
{
optional<int> rhs(3);
test<Z>(std::move(rhs), true);
}
return 0;
}
@@ -0,0 +1,149 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// UNSUPPORTED: c++03, c++11, c++14
// <optional>
// template <class... Args>
// constexpr explicit optional(in_place_t, Args&&... args);
#include <optional>
#include <type_traits>
#include <cassert>
#include "test_macros.h"
using std::optional;
using std::in_place_t;
using std::in_place;
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
{
public:
Z(int) {TEST_THROW(6);}
};
int main(int, char**)
{
{
constexpr optional<int> opt(in_place, 5);
static_assert(static_cast<bool>(opt) == true, "");
static_assert(*opt == 5, "");
struct test_constexpr_ctor
: public optional<int>
{
constexpr test_constexpr_ctor(in_place_t, int i)
: optional<int>(in_place, i) {}
};
}
{
optional<const int> opt(in_place, 5);
assert(*opt == 5);
}
{
const optional<X> opt(in_place);
assert(static_cast<bool>(opt) == true);
assert(*opt == X());
}
{
const optional<X> opt(in_place, 5);
assert(static_cast<bool>(opt) == true);
assert(*opt == X(5));
}
{
const optional<X> opt(in_place, 5, 4);
assert(static_cast<bool>(opt) == true);
assert(*opt == X(5, 4));
}
{
constexpr optional<Y> opt(in_place);
static_assert(static_cast<bool>(opt) == true, "");
static_assert(*opt == Y(), "");
struct test_constexpr_ctor
: public optional<Y>
{
constexpr test_constexpr_ctor(in_place_t)
: optional<Y>(in_place) {}
};
}
{
constexpr optional<Y> opt(in_place, 5);
static_assert(static_cast<bool>(opt) == true, "");
static_assert(*opt == Y(5), "");
struct test_constexpr_ctor
: public optional<Y>
{
constexpr test_constexpr_ctor(in_place_t, int i)
: optional<Y>(in_place, i) {}
};
}
{
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 optional<Y>
{
constexpr test_constexpr_ctor(in_place_t, int i, int j)
: optional<Y>(in_place, i, j) {}
};
}
#ifndef TEST_HAS_NO_EXCEPTIONS
{
try
{
const optional<Z> opt(in_place, 1);
assert(false);
}
catch (int i)
{
assert(i == 6);
}
}
#endif
return 0;
}
@@ -0,0 +1,117 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14
// <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>
#include "test_macros.h"
using std::optional;
using std::in_place_t;
using std::in_place;
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:
Z() : i_(0) {}
Z(int i) : i_(i) {}
Z(std::initializer_list<int> il) : i_(il.begin()[0]), j_(il.begin()[1])
{TEST_THROW(6);}
friend bool operator==(const Z& x, const Z& y)
{return x.i_ == y.i_ && x.j_ == y.j_;}
};
int main(int, char**)
{
{
static_assert(!std::is_constructible<X, std::initializer_list<int>&>::value, "");
static_assert(!std::is_constructible<optional<X>, std::initializer_list<int>&>::value, "");
}
{
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);
}
{
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<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 optional<Y>
{
constexpr test_constexpr_ctor(in_place_t, std::initializer_list<int> i)
: optional<Y>(in_place, i) {}
};
}
#ifndef TEST_HAS_NO_EXCEPTIONS
{
static_assert(std::is_constructible<optional<Z>, std::initializer_list<int>&>::value, "");
try
{
optional<Z> opt(in_place, {3, 1});
assert(false);
}
catch (int i)
{
assert(i == 6);
}
}
#endif
return 0;
}
@@ -0,0 +1,53 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// REQUIRES: c++17
// <optional>
// constexpr optional(const optional<T>&& rhs);
// C++17 said:
// If is_trivially_move_constructible_v<T> is true,
// this constructor shall be a constexpr constructor.
//
// P0602 changed this to:
// If is_trivially_move_constructible_v<T> is true, this constructor is trivial.
//
// which means that it can't be constexpr if T is not trivially move-constructible,
// because you have to do a placement new to get the value into place.
// Except in the case where it is moving from an empty optional - that could be
// made to be constexpr (and libstdc++ does so).
#include <optional>
#include <type_traits>
#include <cassert>
#include "test_macros.h"
struct S {
constexpr S() : v_(0) {}
S(int v) : v_(v) {}
constexpr S(const S &rhs) : v_(rhs.v_) {} // not trivially moveable
constexpr S( S &&rhs) : v_(rhs.v_) {} // not trivially moveable
int v_;
};
constexpr bool test() // expected-error {{constexpr function never produces a constant expression}}
{
std::optional<S> o1{3};
std::optional<S> o2 = std::move(o1);
return o2.has_value(); // return -something-
}
int main(int, char**)
{
static_assert (!std::is_trivially_move_constructible_v<S>, "" );
static_assert (test(), ""); // expected-error-re {{{{(static_assert|static assertion)}} expression is not an integral constant expression}}
return 0;
}
@@ -0,0 +1,225 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14
// Throwing bad_optional_access is supported starting in macosx10.13
// XFAIL: use_system_cxx_lib && target={{.+}}-apple-macosx10.{{9|10|11|12}} && !no-exceptions
// <optional>
// constexpr optional(optional<T>&& rhs);
#include <optional>
#include <type_traits>
#include <cassert>
#include "test_macros.h"
#include "archetypes.h"
using std::optional;
template <class T, class ...InitArgs>
void test(InitArgs&&... args)
{
const optional<T> orig(std::forward<InitArgs>(args)...);
optional<T> rhs(orig);
bool rhs_engaged = static_cast<bool>(rhs);
optional<T> lhs = std::move(rhs);
assert(static_cast<bool>(lhs) == rhs_engaged);
if (rhs_engaged)
assert(*lhs == *orig);
}
template <class T, class ...InitArgs>
constexpr bool constexpr_test(InitArgs&&... args)
{
static_assert( std::is_trivially_copy_constructible_v<T>, ""); // requirement
const optional<T> orig(std::forward<InitArgs>(args)...);
optional<T> rhs(orig);
optional<T> lhs = std::move(rhs);
return (lhs.has_value() == orig.has_value()) &&
(lhs.has_value() ? *lhs == *orig : true);
}
void test_throwing_ctor() {
#ifndef TEST_HAS_NO_EXCEPTIONS
struct Z {
Z() : count(0) {}
Z(Z&& o) : count(o.count + 1)
{ if (count == 2) throw 6; }
int count;
};
Z z;
optional<Z> rhs(std::move(z));
try
{
optional<Z> lhs(std::move(rhs));
assert(false);
}
catch (int i)
{
assert(i == 6);
}
#endif
}
template <class T, class ...InitArgs>
void test_ref(InitArgs&&... args)
{
optional<T> rhs(std::forward<InitArgs>(args)...);
bool rhs_engaged = static_cast<bool>(rhs);
optional<T> lhs = std::move(rhs);
assert(static_cast<bool>(lhs) == rhs_engaged);
if (rhs_engaged)
assert(&(*lhs) == &(*rhs));
}
void test_reference_extension()
{
#if defined(_LIBCPP_VERSION) && 0 // FIXME these extensions are currently disabled.
using T = TestTypes::TestType;
T::reset();
{
T t;
T::reset_constructors();
test_ref<T&>();
test_ref<T&>(t);
assert(T::alive == 1);
assert(T::constructed == 0);
assert(T::assigned == 0);
assert(T::destroyed == 0);
}
assert(T::destroyed == 1);
assert(T::alive == 0);
{
T t;
const T& ct = t;
T::reset_constructors();
test_ref<T const&>();
test_ref<T const&>(t);
test_ref<T const&>(ct);
assert(T::alive == 1);
assert(T::constructed == 0);
assert(T::assigned == 0);
assert(T::destroyed == 0);
}
assert(T::alive == 0);
assert(T::destroyed == 1);
{
T t;
T::reset_constructors();
test_ref<T&&>();
test_ref<T&&>(std::move(t));
assert(T::alive == 1);
assert(T::constructed == 0);
assert(T::assigned == 0);
assert(T::destroyed == 0);
}
assert(T::alive == 0);
assert(T::destroyed == 1);
{
T t;
const T& ct = t;
T::reset_constructors();
test_ref<T const&&>();
test_ref<T const&&>(std::move(t));
test_ref<T const&&>(std::move(ct));
assert(T::alive == 1);
assert(T::constructed == 0);
assert(T::assigned == 0);
assert(T::destroyed == 0);
}
assert(T::alive == 0);
assert(T::destroyed == 1);
{
static_assert(!std::is_copy_constructible<std::optional<T&&>>::value, "");
static_assert(!std::is_copy_constructible<std::optional<T const&&>>::value, "");
}
#endif
}
int main(int, char**)
{
test<int>();
test<int>(3);
static_assert(constexpr_test<int>(), "" );
static_assert(constexpr_test<int>(3), "" );
{
optional<const int> o(42);
optional<const int> o2(std::move(o));
assert(*o2 == 42);
}
{
using T = TestTypes::TestType;
T::reset();
optional<T> rhs;
assert(T::alive == 0);
const optional<T> lhs(std::move(rhs));
assert(lhs.has_value() == false);
assert(rhs.has_value() == false);
assert(T::alive == 0);
}
TestTypes::TestType::reset();
{
using T = TestTypes::TestType;
T::reset();
optional<T> rhs(42);
assert(T::alive == 1);
assert(T::value_constructed == 1);
assert(T::move_constructed == 0);
const optional<T> lhs(std::move(rhs));
assert(lhs.has_value());
assert(rhs.has_value());
assert(lhs.value().value == 42);
assert(rhs.value().value == -1);
assert(T::move_constructed == 1);
assert(T::alive == 2);
}
TestTypes::TestType::reset();
{
using namespace ConstexprTestTypes;
test<TestType>();
test<TestType>(42);
}
{
using namespace TrivialTestTypes;
test<TestType>();
test<TestType>(42);
}
{
test_throwing_ctor();
}
{
struct ThrowsMove {
ThrowsMove() noexcept(false) {}
ThrowsMove(ThrowsMove const&) noexcept(false) {}
ThrowsMove(ThrowsMove &&) noexcept(false) {}
};
static_assert(!std::is_nothrow_move_constructible<optional<ThrowsMove>>::value, "");
struct NoThrowMove {
NoThrowMove() noexcept(false) {}
NoThrowMove(NoThrowMove const&) noexcept(false) {}
NoThrowMove(NoThrowMove &&) noexcept(true) {}
};
static_assert(std::is_nothrow_move_constructible<optional<NoThrowMove>>::value, "");
}
{
test_reference_extension();
}
{
constexpr std::optional<int> o1{4};
constexpr std::optional<int> o2 = std::move(o1);
static_assert( *o2 == 4, "" );
}
return 0;
}
@@ -0,0 +1,76 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14
// <optional>
// constexpr optional(nullopt_t) noexcept;
#include <optional>
#include <type_traits>
#include <cassert>
#include "archetypes.h"
#include "test_macros.h"
using std::optional;
using std::nullopt_t;
using std::nullopt;
template <class Opt>
void
test_constexpr()
{
static_assert(std::is_nothrow_constructible<Opt, nullopt_t&>::value, "");
static_assert(std::is_trivially_destructible<Opt>::value, "");
static_assert(std::is_trivially_destructible<typename Opt::value_type>::value, "");
constexpr Opt opt(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(std::is_nothrow_constructible<Opt, nullopt_t&>::value, "");
static_assert(!std::is_trivially_destructible<Opt>::value, "");
static_assert(!std::is_trivially_destructible<typename Opt::value_type>::value, "");
{
Opt opt(nullopt);
assert(static_cast<bool>(opt) == false);
}
{
const Opt opt(nullopt);
assert(static_cast<bool>(opt) == false);
}
struct test_constexpr_ctor
: public Opt
{
constexpr test_constexpr_ctor() {}
};
}
int main(int, char**)
{
test_constexpr<optional<int>>();
test_constexpr<optional<int*>>();
test_constexpr<optional<ImplicitTypes::NoCtors>>();
test_constexpr<optional<NonTrivialTypes::NoCtors>>();
test_constexpr<optional<NonConstexprTypes::NoCtors>>();
test<optional<NonLiteralTypes::NoCtors>>();
return 0;
}
@@ -0,0 +1,97 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14
// <optional>
// template <class U>
// optional(optional<U>&& rhs);
#include <cassert>
#include <memory>
#include <optional>
#include <type_traits>
#include <utility>
#include "test_macros.h"
using std::optional;
template <class T, class U>
TEST_CONSTEXPR_CXX20 void
test(optional<U>&& rhs, bool is_going_to_throw = false)
{
bool rhs_engaged = static_cast<bool>(rhs);
#ifndef TEST_HAS_NO_EXCEPTIONS
try
{
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);
}
#else
if (is_going_to_throw) return;
optional<T> lhs = std::move(rhs);
assert(static_cast<bool>(lhs) == rhs_engaged);
#endif
}
class X
{
int i_;
public:
TEST_CONSTEXPR_CXX20 X(int i) : i_(i) {}
TEST_CONSTEXPR_CXX20 X(X&& x) : i_(std::exchange(x.i_, 0)) {}
TEST_CONSTEXPR_CXX20 ~X() {i_ = 0;}
friend constexpr bool operator==(const X& x, const X& y) {return x.i_ == y.i_;}
};
struct Z
{
Z(int) { TEST_THROW(6); }
};
template<class T, class U>
TEST_CONSTEXPR_CXX20 bool test_all()
{
{
optional<T> rhs;
test<U>(std::move(rhs));
}
{
optional<T> rhs(short{3});
test<U>(std::move(rhs));
}
return true;
}
int main(int, char**)
{
test_all<short, int>();
test_all<int, X>();
#if TEST_STD_VER > 17
static_assert(test_all<short, int>());
static_assert(test_all<int, X>());
#endif
{
optional<int> rhs;
test<Z>(std::move(rhs));
}
{
optional<int> rhs(3);
test<Z>(std::move(rhs), true);
}
static_assert(!(std::is_constructible<optional<X>, optional<Z>>::value), "");
return 0;
}
@@ -0,0 +1,152 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// UNSUPPORTED: c++03, c++11, c++14
// Throwing bad_optional_access is supported starting in macosx10.13
// XFAIL: use_system_cxx_lib && target={{.+}}-apple-macosx10.{{9|10|11|12}} && !no-exceptions
// <optional>
// constexpr optional(T&& v);
#include <optional>
#include <type_traits>
#include <cassert>
#include "test_macros.h"
#include "archetypes.h"
using std::optional;
class Z
{
public:
Z(int) {}
Z(Z&&) {TEST_THROW(6);}
};
int main(int, char**)
{
{
typedef int T;
constexpr optional<T> opt(T(5));
static_assert(static_cast<bool>(opt) == true, "");
static_assert(*opt == 5, "");
struct test_constexpr_ctor
: public optional<T>
{
constexpr test_constexpr_ctor(T&&) {}
};
}
{
typedef double T;
constexpr optional<T> opt(T(3));
static_assert(static_cast<bool>(opt) == true, "");
static_assert(*opt == 3, "");
struct test_constexpr_ctor
: public optional<T>
{
constexpr test_constexpr_ctor(T&&) {}
};
}
{
const int x = 42;
optional<const int> o(std::move(x));
assert(*o == 42);
}
{
typedef TestTypes::TestType T;
T::reset();
optional<T> opt = T{3};
assert(T::alive == 1);
assert(T::move_constructed == 1);
assert(static_cast<bool>(opt) == true);
assert(opt.value().value == 3);
}
{
typedef ExplicitTestTypes::TestType T;
static_assert(!std::is_convertible<T&&, optional<T>>::value, "");
T::reset();
optional<T> opt(T{3});
assert(T::alive == 1);
assert(T::move_constructed == 1);
assert(static_cast<bool>(opt) == true);
assert(opt.value().value == 3);
}
{
typedef TestTypes::TestType T;
T::reset();
optional<T> opt = {3};
assert(T::alive == 1);
assert(T::value_constructed == 1);
assert(T::copy_constructed == 0);
assert(T::move_constructed == 0);
assert(static_cast<bool>(opt) == true);
assert(opt.value().value == 3);
}
{
typedef ConstexprTestTypes::TestType T;
constexpr optional<T> opt = {T(3)};
static_assert(static_cast<bool>(opt) == true, "");
static_assert(opt.value().value == 3, "");
struct test_constexpr_ctor
: public optional<T>
{
constexpr test_constexpr_ctor(const T&) {}
};
}
{
typedef ConstexprTestTypes::TestType T;
constexpr optional<T> opt = {3};
static_assert(static_cast<bool>(opt) == true, "");
static_assert(opt.value().value == 3, "");
struct test_constexpr_ctor
: public optional<T>
{
constexpr test_constexpr_ctor(const T&) {}
};
}
{
typedef ExplicitConstexprTestTypes::TestType T;
static_assert(!std::is_convertible<T&&, optional<T>>::value, "");
constexpr optional<T> opt(T{3});
static_assert(static_cast<bool>(opt) == true, "");
static_assert(opt.value().value == 3, "");
struct test_constexpr_ctor
: public optional<T>
{
constexpr test_constexpr_ctor(T&&) {}
};
}
#ifndef TEST_HAS_NO_EXCEPTIONS
{
try
{
Z z(3);
optional<Z> opt(std::move(z));
assert(false);
}
catch (int i)
{
assert(i == 6);
}
}
#endif
return 0;
}
@@ -0,0 +1,67 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14
// <optional>
// ~optional();
#include <optional>
#include <type_traits>
#include <cassert>
#include "test_macros.h"
using std::optional;
struct PODType {
int value;
int value2;
};
class X
{
public:
static bool dtor_called;
X() = default;
~X() {dtor_called = true;}
};
bool X::dtor_called = false;
int main(int, char**)
{
{
typedef int T;
static_assert(std::is_trivially_destructible<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<optional<T>>::value, "");
}
{
typedef PODType T;
static_assert(std::is_trivially_destructible<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<optional<T>>::value, "");
{
X x;
optional<X> opt{x};
assert(X::dtor_called == false);
}
assert(X::dtor_called == true);
}
return 0;
}
@@ -0,0 +1,71 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14
// <optional>
// void reset() noexcept;
#include <optional>
#include <type_traits>
#include <cassert>
#include "test_macros.h"
using std::optional;
struct X
{
static bool dtor_called;
~X() {dtor_called = true;}
};
bool X::dtor_called = false;
constexpr bool check_reset()
{
{
optional<int> opt;
static_assert(noexcept(opt.reset()) == true, "");
opt.reset();
assert(static_cast<bool>(opt) == false);
}
{
optional<int> opt(3);
opt.reset();
assert(static_cast<bool>(opt) == false);
}
return true;
}
int main(int, char**)
{
check_reset();
#if TEST_STD_VER >= 20
static_assert(check_reset());
#endif
{
optional<X> opt;
static_assert(noexcept(opt.reset()) == true, "");
assert(X::dtor_called == false);
opt.reset();
assert(X::dtor_called == false);
assert(static_cast<bool>(opt) == false);
}
{
optional<X> opt(X{});
X::dtor_called = false;
opt.reset();
assert(X::dtor_called == true);
assert(static_cast<bool>(opt) == false);
X::dtor_called = false;
}
return 0;
}
@@ -0,0 +1,38 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14
// <optional>
// constexpr explicit optional<T>::operator bool() const noexcept;
#include <optional>
#include <type_traits>
#include <cassert>
#include "test_macros.h"
int main(int, char**)
{
using std::optional;
{
const optional<int> opt; ((void)opt);
ASSERT_NOEXCEPT(bool(opt));
static_assert(!std::is_convertible<optional<int>, bool>::value, "");
}
{
constexpr optional<int> opt;
static_assert(!opt, "");
}
{
constexpr optional<int> opt(0);
static_assert(opt, "");
}
return 0;
}
@@ -0,0 +1,64 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14
// <optional>
// constexpr T& optional<T>::operator*() &;
#include <optional>
#include <type_traits>
#include <cassert>
#include "test_macros.h"
using std::optional;
struct X
{
constexpr int test() const& {return 3;}
int test() & {return 4;}
constexpr int test() const&& {return 5;}
int test() && {return 6;}
};
struct Y
{
constexpr int test() {return 7;}
};
constexpr int
test()
{
optional<Y> opt{Y{}};
return (*opt).test();
}
int main(int, char**)
{
{
optional<X> opt; ((void)opt);
ASSERT_SAME_TYPE(decltype(*opt), X&);
LIBCPP_STATIC_ASSERT(noexcept(*opt));
// ASSERT_NOT_NOEXCEPT(*opt);
// FIXME: This assertion fails with GCC because it can see that
// (A) operator*() is constexpr, and
// (B) there is no path through the function that throws.
// It's arguable if this is the correct behavior for the noexcept
// operator.
// Regardless this function should still be noexcept(false) because
// it has a narrow contract.
}
{
optional<X> opt(X{});
assert((*opt).test() == 4);
}
static_assert(test() == 7, "");
return 0;
}
@@ -0,0 +1,60 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14
// <optional>
// constexpr const T& optional<T>::operator*() const &;
#include <optional>
#include <type_traits>
#include <cassert>
#include "test_macros.h"
using std::optional;
struct X
{
constexpr int test() const& {return 3;}
int test() & {return 4;}
constexpr int test() const&& {return 5;}
int test() && {return 6;}
};
struct Y
{
int test() const {return 2;}
};
int main(int, char**)
{
{
const optional<X> opt; ((void)opt);
ASSERT_SAME_TYPE(decltype(*opt), X const&);
LIBCPP_STATIC_ASSERT(noexcept(*opt));
// ASSERT_NOT_NOEXCEPT(*opt);
// FIXME: This assertion fails with GCC because it can see that
// (A) operator*() is constexpr, and
// (B) there is no path through the function that throws.
// It's arguable if this is the correct behavior for the noexcept
// operator.
// Regardless this function should still be noexcept(false) because
// it has a narrow contract.
}
{
constexpr optional<X> opt(X{});
static_assert((*opt).test() == 3, "");
}
{
constexpr optional<Y> opt(Y{});
assert((*opt).test() == 2);
}
return 0;
}
@@ -0,0 +1,60 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14
// <optional>
// constexpr T&& optional<T>::operator*() const &&;
#include <optional>
#include <type_traits>
#include <cassert>
#include "test_macros.h"
using std::optional;
struct X
{
constexpr int test() const& {return 3;}
int test() & {return 4;}
constexpr int test() const&& {return 5;}
int test() && {return 6;}
};
struct Y
{
int test() const && {return 2;}
};
int main(int, char**)
{
{
const optional<X> opt; ((void)opt);
ASSERT_SAME_TYPE(decltype(*std::move(opt)), X const &&);
LIBCPP_STATIC_ASSERT(noexcept(*opt));
// ASSERT_NOT_NOEXCEPT(*std::move(opt));
// FIXME: This assertion fails with GCC because it can see that
// (A) operator*() is constexpr, and
// (B) there is no path through the function that throws.
// It's arguable if this is the correct behavior for the noexcept
// operator.
// Regardless this function should still be noexcept(false) because
// it has a narrow contract.
}
{
constexpr optional<X> opt(X{});
static_assert((*std::move(opt)).test() == 5, "");
}
{
constexpr optional<Y> opt(Y{});
assert((*std::move(opt)).test() == 2);
}
return 0;
}
@@ -0,0 +1,64 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14
// <optional>
// constexpr T&& optional<T>::operator*() &&;
#include <optional>
#include <type_traits>
#include <cassert>
#include "test_macros.h"
using std::optional;
struct X
{
constexpr int test() const& {return 3;}
int test() & {return 4;}
constexpr int test() const&& {return 5;}
int test() && {return 6;}
};
struct Y
{
constexpr int test() && {return 7;}
};
constexpr int
test()
{
optional<Y> opt{Y{}};
return (*std::move(opt)).test();
}
int main(int, char**)
{
{
optional<X> opt; ((void)opt);
ASSERT_SAME_TYPE(decltype(*std::move(opt)), X&&);
LIBCPP_STATIC_ASSERT(noexcept(*opt));
// ASSERT_NOT_NOEXCEPT(*std::move(opt));
// FIXME: This assertion fails with GCC because it can see that
// (A) operator*() is constexpr, and
// (B) there is no path through the function that throws.
// It's arguable if this is the correct behavior for the noexcept
// operator.
// Regardless this function should still be noexcept(false) because
// it has a narrow contract.
}
{
optional<X> opt(X{});
assert((*std::move(opt)).test() == 6);
}
static_assert(test() == 7, "");
return 0;
}
@@ -0,0 +1,38 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14
// <optional>
// constexpr bool optional<T>::has_value() const noexcept;
#include <optional>
#include <type_traits>
#include <cassert>
#include "test_macros.h"
int main(int, char**)
{
using std::optional;
{
const optional<int> opt; ((void)opt);
ASSERT_NOEXCEPT(opt.has_value());
ASSERT_SAME_TYPE(decltype(opt.has_value()), bool);
}
{
constexpr optional<int> opt;
static_assert(!opt.has_value(), "");
}
{
constexpr optional<int> opt(0);
static_assert(opt.has_value(), "");
}
return 0;
}
@@ -0,0 +1,62 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14
// <optional>
// constexpr T* optional<T>::operator->();
#include <optional>
#include <type_traits>
#include <cassert>
#include "test_macros.h"
using std::optional;
struct X
{
int test() noexcept {return 3;}
};
struct Y
{
constexpr int test() {return 3;}
};
constexpr int
test()
{
optional<Y> opt{Y{}};
return opt->test();
}
int main(int, char**)
{
{
std::optional<X> opt; ((void)opt);
ASSERT_SAME_TYPE(decltype(opt.operator->()), X*);
// ASSERT_NOT_NOEXCEPT(opt.operator->());
// FIXME: This assertion fails with GCC because it can see that
// (A) operator->() is constexpr, and
// (B) there is no path through the function that throws.
// It's arguable if this is the correct behavior for the noexcept
// operator.
// Regardless this function should still be noexcept(false) because
// it has a narrow contract.
}
{
optional<X> opt(X{});
assert(opt->test() == 3);
}
{
static_assert(test() == 3, "");
}
return 0;
}
@@ -0,0 +1,66 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14
// <optional>
// constexpr const T* optional<T>::operator->() const;
#include <optional>
#include <type_traits>
#include <cassert>
#include "test_macros.h"
using std::optional;
struct X
{
constexpr int test() const {return 3;}
};
struct Y
{
int test() const noexcept {return 2;}
};
struct Z
{
const Z* operator&() const;
constexpr int test() const {return 1;}
};
int main(int, char**)
{
{
const std::optional<X> opt; ((void)opt);
ASSERT_SAME_TYPE(decltype(opt.operator->()), X const*);
// ASSERT_NOT_NOEXCEPT(opt.operator->());
// FIXME: This assertion fails with GCC because it can see that
// (A) operator->() is constexpr, and
// (B) there is no path through the function that throws.
// It's arguable if this is the correct behavior for the noexcept
// operator.
// Regardless this function should still be noexcept(false) because
// it has a narrow contract.
}
{
constexpr optional<X> opt(X{});
static_assert(opt->test() == 3, "");
}
{
constexpr optional<Y> opt(Y{});
assert(opt->test() == 2);
}
{
constexpr optional<Z> opt(Z{});
static_assert(opt->test() == 1, "");
}
return 0;
}
@@ -0,0 +1,78 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14
// Throwing bad_optional_access is supported starting in macosx10.13
// XFAIL: use_system_cxx_lib && target={{.+}}-apple-macosx10.{{9|10|11|12}} && !no-exceptions
// <optional>
// constexpr T& optional<T>::value() &;
#include <optional>
#include <type_traits>
#include <cassert>
#include "test_macros.h"
using std::optional;
using std::bad_optional_access;
struct X
{
X() = default;
X(const X&) = delete;
constexpr int test() const & {return 3;}
int test() & {return 4;}
constexpr int test() const && {return 5;}
int test() && {return 6;}
};
struct Y
{
constexpr int test() & {return 7;}
};
constexpr int
test()
{
optional<Y> opt{Y{}};
return opt.value().test();
}
int main(int, char**)
{
{
optional<X> opt; ((void)opt);
ASSERT_NOT_NOEXCEPT(opt.value());
ASSERT_SAME_TYPE(decltype(opt.value()), X&);
}
{
optional<X> opt;
opt.emplace();
assert(opt.value().test() == 4);
}
#ifndef TEST_HAS_NO_EXCEPTIONS
{
optional<X> opt;
try
{
(void)opt.value();
assert(false);
}
catch (const bad_optional_access&)
{
}
}
#endif
static_assert(test() == 7, "");
return 0;
}
@@ -0,0 +1,34 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14
// <optional>
// constexpr const T& optional<T>::value() const &;
#include <optional>
#include <type_traits>
#include <cassert>
using std::optional;
struct X
{
constexpr int test() const {return 3;}
int test() {return 4;}
};
int main(int, char**)
{
{
constexpr optional<X> opt;
static_assert(opt.value().test() == 3, "");
}
return 0;
}
@@ -0,0 +1,69 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14
// Throwing bad_optional_access is supported starting in macosx10.13
// XFAIL: use_system_cxx_lib && target={{.+}}-apple-macosx10.{{9|10|11|12}} && !no-exceptions
// <optional>
// constexpr const T& optional<T>::value() const &;
#include <optional>
#include <type_traits>
#include <cassert>
#include "test_macros.h"
using std::optional;
using std::in_place_t;
using std::in_place;
using std::bad_optional_access;
struct X
{
X() = default;
X(const X&) = delete;
constexpr int test() const & {return 3;}
int test() & {return 4;}
constexpr int test() const && {return 5;}
int test() && {return 6;}
};
int main(int, char**)
{
{
const optional<X> opt; ((void)opt);
ASSERT_NOT_NOEXCEPT(opt.value());
ASSERT_SAME_TYPE(decltype(opt.value()), X const&);
}
{
constexpr optional<X> opt(in_place);
static_assert(opt.value().test() == 3, "");
}
{
const optional<X> opt(in_place);
assert(opt.value().test() == 3);
}
#ifndef TEST_HAS_NO_EXCEPTIONS
{
const optional<X> opt;
try
{
(void)opt.value();
assert(false);
}
catch (const bad_optional_access&)
{
}
}
#endif
return 0;
}
@@ -0,0 +1,69 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14
// Throwing bad_optional_access is supported starting in macosx10.13
// XFAIL: use_system_cxx_lib && target={{.+}}-apple-macosx10.{{9|10|11|12}} && !no-exceptions
// <optional>
// constexpr const T& optional<T>::value() const &&;
#include <optional>
#include <type_traits>
#include <cassert>
#include "test_macros.h"
using std::optional;
using std::in_place_t;
using std::in_place;
using std::bad_optional_access;
struct X
{
X() = default;
X(const X&) = delete;
constexpr int test() const & {return 3;}
int test() & {return 4;}
constexpr int test() const && {return 5;}
int test() && {return 6;}
};
int main(int, char**)
{
{
const optional<X> opt; ((void)opt);
ASSERT_NOT_NOEXCEPT(std::move(opt).value());
ASSERT_SAME_TYPE(decltype(std::move(opt).value()), X const&&);
}
{
constexpr optional<X> opt(in_place);
static_assert(std::move(opt).value().test() == 5, "");
}
{
const optional<X> opt(in_place);
assert(std::move(opt).value().test() == 5);
}
#ifndef TEST_HAS_NO_EXCEPTIONS
{
const optional<X> opt;
try
{
(void)std::move(opt).value();
assert(false);
}
catch (const bad_optional_access&)
{
}
}
#endif
return 0;
}
@@ -0,0 +1,75 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14
// <optional>
// template <class U> constexpr T optional<T>::value_or(U&& v) &&;
#include <optional>
#include <type_traits>
#include <cassert>
#include "test_macros.h"
using std::optional;
using std::in_place_t;
using std::in_place;
struct Y
{
int i_;
constexpr Y(int i) : i_(i) {}
};
struct X
{
int i_;
constexpr X(int i) : i_(i) {}
constexpr X(X&& x) : i_(x.i_) {x.i_ = 0;}
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_;}
};
constexpr int test()
{
{
optional<X> opt(in_place, 2);
Y y(3);
assert(std::move(opt).value_or(y) == 2);
assert(*opt == 0);
}
{
optional<X> opt(in_place, 2);
assert(std::move(opt).value_or(Y(3)) == 2);
assert(*opt == 0);
}
{
optional<X> opt;
Y y(3);
assert(std::move(opt).value_or(y) == 3);
assert(!opt);
}
{
optional<X> opt;
assert(std::move(opt).value_or(Y(3)) == 4);
assert(!opt);
}
return 0;
}
int main(int, char**)
{
static_assert(test() == 0);
return 0;
}
@@ -0,0 +1,80 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14
// <optional>
// template <class U> constexpr T optional<T>::value_or(U&& v) const&;
#include <optional>
#include <type_traits>
#include <cassert>
#include "test_macros.h"
using std::optional;
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_;}
};
int main(int, char**)
{
{
constexpr optional<X> opt(2);
constexpr Y y(3);
static_assert(opt.value_or(y) == 2, "");
}
{
constexpr optional<X> opt(2);
static_assert(opt.value_or(Y(3)) == 2, "");
}
{
constexpr optional<X> opt;
constexpr Y y(3);
static_assert(opt.value_or(y) == 3, "");
}
{
constexpr optional<X> opt;
static_assert(opt.value_or(Y(3)) == 4, "");
}
{
const optional<X> opt(2);
const Y y(3);
assert(opt.value_or(y) == 2);
}
{
const optional<X> opt(2);
assert(opt.value_or(Y(3)) == 2);
}
{
const optional<X> opt;
const Y y(3);
assert(opt.value_or(y) == 3);
}
{
const optional<X> opt;
assert(opt.value_or(Y(3)) == 4);
}
return 0;
}
@@ -0,0 +1,76 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14
// <optional>
// Throwing bad_optional_access is supported starting in macosx10.13
// XFAIL: use_system_cxx_lib && target={{.+}}-apple-macosx10.{{9|10|11|12}} && !no-exceptions
// constexpr T& optional<T>::value() &&;
#include <optional>
#include <type_traits>
#include <cassert>
#include "test_macros.h"
using std::optional;
using std::bad_optional_access;
struct X
{
X() = default;
X(const X&) = delete;
constexpr int test() const & {return 3;}
int test() & {return 4;}
constexpr int test() const && {return 5;}
int test() && {return 6;}
};
struct Y
{
constexpr int test() && {return 7;}
};
constexpr int
test()
{
optional<Y> opt{Y{}};
return std::move(opt).value().test();
}
int main(int, char**)
{
{
optional<X> opt; ((void)opt);
ASSERT_NOT_NOEXCEPT(std::move(opt).value());
ASSERT_SAME_TYPE(decltype(std::move(opt).value()), X&&);
}
{
optional<X> opt;
opt.emplace();
assert(std::move(opt).value().test() == 6);
}
#ifndef TEST_HAS_NO_EXCEPTIONS
{
optional<X> opt;
try
{
(void)std::move(opt).value();
assert(false);
}
catch (const bad_optional_access&)
{
}
}
#endif
static_assert(test() == 7, "");
return 0;
}
@@ -0,0 +1,328 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14
// <optional>
// void swap(optional&)
// noexcept(is_nothrow_move_constructible<T>::value &&
// is_nothrow_swappable<T>::value)
#include <optional>
#include <type_traits>
#include <cassert>
#include "test_macros.h"
#include "archetypes.h"
using std::optional;
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&&) {TEST_THROW(7);}
friend constexpr bool operator==(const Z& x, const Z& y) {return x.i_ == y.i_;}
friend void swap(Z&, Z&) {TEST_THROW(6);}
};
class W
{
int i_;
public:
constexpr W(int i) : i_(i) {}
friend constexpr bool operator==(const W& x, const W& y) {return x.i_ == y.i_;}
friend TEST_CONSTEXPR_CXX20 void swap(W& x, W& y) noexcept {std::swap(x.i_, y.i_);}
};
template<class T>
TEST_CONSTEXPR_CXX20 bool check_swap()
{
{
optional<T> opt1;
optional<T> 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);
}
{
optional<T> opt1(1);
optional<T> 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);
}
{
optional<T> opt1;
optional<T> 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);
}
{
optional<T> opt1(1);
optional<T> 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);
}
return true;
}
int main(int, char**)
{
check_swap<int>();
check_swap<W>();
#if TEST_STD_VER > 17
static_assert(check_swap<int>());
static_assert(check_swap<W>());
#endif
{
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);
opt1.swap(opt2);
assert(static_cast<bool>(opt1) == false);
assert(static_cast<bool>(opt2) == false);
assert(X::dtor_called == 0);
}
{
optional<X> opt1(1);
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);
}
{
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);
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);
}
{
optional<X> opt1(1);
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);
}
{
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);
opt1.swap(opt2);
assert(static_cast<bool>(opt1) == false);
assert(static_cast<bool>(opt2) == false);
assert(Y::dtor_called == 0);
}
{
optional<Y> opt1(1);
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);
}
{
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);
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);
}
{
optional<Y> opt1(1);
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);
}
{
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);
opt1.swap(opt2);
assert(static_cast<bool>(opt1) == false);
assert(static_cast<bool>(opt2) == false);
}
#ifndef TEST_HAS_NO_EXCEPTIONS
{
optional<Z> opt1;
opt1.emplace(1);
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);
}
{
optional<Z> opt1;
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);
}
{
optional<Z> opt1;
opt1.emplace(1);
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
return 0;
}
@@ -0,0 +1,51 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14
// <optional>
// T shall be an object type and shall satisfy the requirements of Destructible
#include <optional>
using std::optional;
struct X
{
private:
~X() {}
};
int main(int, char**)
{
using std::optional;
{
// expected-error-re@optional:* 2 {{{{(static_assert|static assertion)}} failed{{.*}}instantiation of optional with a reference type is ill-formed}}
optional<int&> opt1;
optional<int&&> opt2;
}
{
// expected-error-re@optional:* {{{{(static_assert|static assertion)}} failed{{.*}}instantiation of optional with a non-destructible type is ill-formed}}
optional<X> opt3;
}
{
// expected-error-re@optional:* {{{{(static_assert|static assertion)}} failed{{.*}}instantiation of optional with a non-object type is undefined behavior}}
// expected-error-re@optional:* {{{{(static_assert|static assertion)}} failed{{.*}}instantiation of optional with a non-destructible type is ill-formed}}
optional<void()> opt4;
}
{
// expected-error-re@optional:* {{{{(static_assert|static assertion)}} failed{{.*}}instantiation of optional with a non-object type is undefined behavior}}
// expected-error-re@optional:* {{{{(static_assert|static assertion)}} failed{{.*}}instantiation of optional with a non-destructible type is ill-formed}}
// expected-error@optional:* 1+ {{cannot form a reference to 'void'}}
optional<const void> opt4;
}
// FIXME these are garbage diagnostics that Clang should not produce
// expected-error@optional:* 0+ {{is not a base class}}
return 0;
}
@@ -0,0 +1,65 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14
// <optional>
// Make sure we properly generate special member functions for optional<T>
// based on the properties of T itself.
#include <optional>
#include <type_traits>
#include "archetypes.h"
#include "test_macros.h"
template <class T>
struct SpecialMemberTest {
using O = std::optional<T>;
static_assert(std::is_default_constructible_v<O>,
"optional is always default constructible.");
static_assert(std::is_copy_constructible_v<O> == std::is_copy_constructible_v<T>,
"optional<T> is copy constructible if and only if T is copy constructible.");
static_assert(std::is_move_constructible_v<O> ==
(std::is_copy_constructible_v<T> || std::is_move_constructible_v<T>),
"optional<T> is move constructible if and only if T is copy or move constructible.");
static_assert(std::is_copy_assignable_v<O> ==
(std::is_copy_constructible_v<T> && std::is_copy_assignable_v<T>),
"optional<T> is copy assignable if and only if T is both copy "
"constructible and copy assignable.");
static_assert(std::is_move_assignable_v<O> ==
((std::is_move_constructible_v<T> && std::is_move_assignable_v<T>) ||
(std::is_copy_constructible_v<T> && std::is_copy_assignable_v<T>)),
"optional<T> is move assignable if and only if T is both move constructible and "
"move assignable, or both copy constructible and copy assignable.");
};
template <class ...Args> static void sink(Args&&...) {}
template <class ...TestTypes>
struct DoTestsMetafunction {
DoTestsMetafunction() { sink(SpecialMemberTest<TestTypes>{}...); }
};
int main(int, char**) {
sink(
ImplicitTypes::ApplyTypes<DoTestsMetafunction>{},
ExplicitTypes::ApplyTypes<DoTestsMetafunction>{},
NonLiteralTypes::ApplyTypes<DoTestsMetafunction>{},
NonTrivialTypes::ApplyTypes<DoTestsMetafunction>{}
);
return 0;
}
@@ -0,0 +1,99 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14, c++17
// <optional>
// The following special member functions should propagate the triviality of
// the element held in the optional (see P0602R4):
//
// constexpr optional(const optional& rhs);
// constexpr optional(optional&& rhs) noexcept(see below);
// constexpr optional<T>& operator=(const optional& rhs);
// constexpr optional<T>& operator=(optional&& rhs) noexcept(see below);
#include <optional>
#include <type_traits>
#include "archetypes.h"
#include "test_macros.h"
constexpr bool implies(bool p, bool q) {
return !p || q;
}
template <class T>
struct SpecialMemberTest {
using O = std::optional<T>;
static_assert(implies(std::is_trivially_copy_constructible_v<T>,
std::is_trivially_copy_constructible_v<O>),
"optional<T> is trivially copy constructible if T is trivially copy constructible.");
static_assert(implies(std::is_trivially_move_constructible_v<T>,
std::is_trivially_move_constructible_v<O>),
"optional<T> is trivially move constructible if T is trivially move constructible");
static_assert(implies(std::is_trivially_copy_constructible_v<T> &&
std::is_trivially_copy_assignable_v<T> &&
std::is_trivially_destructible_v<T>,
std::is_trivially_copy_assignable_v<O>),
"optional<T> is trivially copy assignable if T is "
"trivially copy constructible, "
"trivially copy assignable, and "
"trivially destructible");
static_assert(implies(std::is_trivially_move_constructible_v<T> &&
std::is_trivially_move_assignable_v<T> &&
std::is_trivially_destructible_v<T>,
std::is_trivially_move_assignable_v<O>),
"optional<T> is trivially move assignable if T is "
"trivially move constructible, "
"trivially move assignable, and"
"trivially destructible.");
};
template <class ...Args> static void sink(Args&&...) {}
template <class ...TestTypes>
struct DoTestsMetafunction {
DoTestsMetafunction() { sink(SpecialMemberTest<TestTypes>{}...); }
};
struct TrivialMoveNonTrivialCopy {
TrivialMoveNonTrivialCopy() = default;
TrivialMoveNonTrivialCopy(const TrivialMoveNonTrivialCopy&) {}
TrivialMoveNonTrivialCopy(TrivialMoveNonTrivialCopy&&) = default;
TrivialMoveNonTrivialCopy& operator=(const TrivialMoveNonTrivialCopy&) { return *this; }
TrivialMoveNonTrivialCopy& operator=(TrivialMoveNonTrivialCopy&&) = default;
};
struct TrivialCopyNonTrivialMove {
TrivialCopyNonTrivialMove() = default;
TrivialCopyNonTrivialMove(const TrivialCopyNonTrivialMove&) = default;
TrivialCopyNonTrivialMove(TrivialCopyNonTrivialMove&&) {}
TrivialCopyNonTrivialMove& operator=(const TrivialCopyNonTrivialMove&) = default;
TrivialCopyNonTrivialMove& operator=(TrivialCopyNonTrivialMove&&) { return *this; }
};
int main(int, char**) {
sink(
ImplicitTypes::ApplyTypes<DoTestsMetafunction>{},
ExplicitTypes::ApplyTypes<DoTestsMetafunction>{},
NonLiteralTypes::ApplyTypes<DoTestsMetafunction>{},
NonTrivialTypes::ApplyTypes<DoTestsMetafunction>{},
DoTestsMetafunction<TrivialMoveNonTrivialCopy, TrivialCopyNonTrivialMove>{}
);
return 0;
}
@@ -0,0 +1,41 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14
// <optional>
// template <class T>
// class optional
// {
// public:
// typedef T value_type;
// ...
#include <optional>
#include <type_traits>
#include "test_macros.h"
using std::optional;
template <class Opt, class T>
void
test()
{
static_assert(std::is_same<typename Opt::value_type, T>::value, "");
}
int main(int, char**)
{
test<optional<int>, int>();
test<optional<const int>, const int>();
test<optional<double>, double>();
test<optional<const double>, const double>();
return 0;
}