Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,18 @@
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//

// UNSUPPORTED: c++03, c++11, c++14

// <optional>

// template <class U>
// constexpr EXPLICIT optional(U&& u);
// constexpr explicit optional(U&& u);

#include <cassert>
#include <optional>
#include <type_traits>

#include "test_macros.h"
#include "archetypes.h"
#include "test_convertible.h"

Expand Down Expand Up @@ -45,80 +44,47 @@ struct ImplicitAny {
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);
assert((test_convertible<O, From>()));
assert(!(test_convertible<O, void*>()));
assert(!(test_convertible<O, From, int>()));
assert(opt);
assert(*opt == static_cast<To>(v));

return true;
}

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, "");
assert((std::is_constructible<O, Input>::value));
assert(!(std::is_convertible<Input, O>::value));
assert(!(std::is_constructible<O, void*>::value));
assert(!(std::is_constructible<O, Input, int>::value));

optional<To> opt(std::forward<Input>(in));
optional<To> opt2{std::forward<Input>(in)};
return opt && *opt == static_cast<To>(v) && (opt2 && *opt2 == static_cast<To>(v));
assert(opt);
assert(opt2);
assert(*opt == static_cast<To>(v));
assert(*opt2 == static_cast<To>(v));

return true;
}

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 T = TestTypes::TestType;
optional<T> opt({3});
assert(opt && *opt == static_cast<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) {
}
using T = TestTypes::TestType;
assert((implicit_conversion<T>(3, T(3))));
}
#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();
Expand All @@ -137,7 +103,20 @@ void test_explicit() {
}
assert(T::alive == 0);
}
}

TEST_CONSTEXPR_CXX26 void test_throwing() {
#ifndef TEST_HAS_NO_EXCEPTIONS
{
try {
using T = ImplicitThrow;
optional<T> t = 42;
assert(false);
((void)t);
} catch (int) {
}
}

{
try {
using T = ExplicitThrow;
Expand All @@ -149,9 +128,68 @@ void test_explicit() {
#endif
}

constexpr bool test() {
{
assert((implicit_conversion<long long>(42, 42)));
}

{
assert((implicit_conversion<long double>(3.14, 3.14)));
}

{
int x = 42;
optional<void* const> o(&x);
assert(*o == &x);
}

{
using T = TrivialTestTypes::TestType;
assert((implicit_conversion<T>(42, 42)));
}

{
using O = optional<ImplicitAny>;
assert(!(test_convertible<O, std::in_place_t>()));
assert(!(test_convertible<O, std::in_place_t&>()));
assert(!(test_convertible<O, const std::in_place_t&>()));
assert(!(test_convertible<O, std::in_place_t&&>()));
assert(!(test_convertible<O, const std::in_place_t&&>()));
}

{
using T = ExplicitTrivialTestTypes::TestType;
assert((explicit_conversion<T>(42, 42)));
}

{
using T = ExplicitConstexprTestTypes::TestType;
assert(explicit_conversion<T>(42, 42));
assert(!(std::is_convertible<int, T>::value));
}

#if TEST_STD_VER >= 26 && 0
test_throwing();
#endif

return true;
}

int main(int, char**) {
test_implicit();
test_explicit();
test();
static_assert(test());

{
test_implicit();
}

{
test_explicit();
}

{
test_throwing();
}

return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//

// UNSUPPORTED: c++03, c++11, c++14

// <optional>
Expand All @@ -20,35 +20,38 @@
#include "archetypes.h"

using std::optional;
template <typename T, typename... Args>
constexpr void test_ctor(Args... args) {
const T t(args...);
const std::optional<T> opt(t);

assert(static_cast<bool>(opt));
assert(*opt == t);

struct test_constexpr_ctor : public optional<T> {
constexpr test_constexpr_ctor(const T&) {}
};
}

constexpr bool test() {
test_ctor<int>(5);
test_ctor<double>(3.0);
test_ctor<const int>(42);
test_ctor<ConstexprTestTypes::TestType>(3);

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);
using T = ExplicitConstexprTestTypes::TestType;
static_assert(!std::is_convertible_v<const T&, optional<T>>);
test_ctor<T>(3);
}

return true;
}

int main(int, char**) {
test();
static_assert(test());

{
typedef TestTypes::TestType T;
T::reset();
Expand All @@ -59,6 +62,7 @@ int main(int, char**) {
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, "");
Expand All @@ -70,29 +74,7 @@ int main(int, char**) {
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++03, c++11, c++14

// <optional>

// template <class U>
Expand Down Expand Up @@ -68,7 +69,7 @@ class Z {
int i_;

public:
Z(int i) : i_(i) { TEST_THROW(6); }
constexpr Z(int i) : i_(i) { TEST_THROW(6); }

friend bool operator==(const Z& x, const Z& y) { return x.i_ == y.i_; }
};
Expand All @@ -86,15 +87,21 @@ constexpr bool test_all() {
return true;
}

int main(int, char**) {
constexpr bool test() {
static_assert(!(std::is_constructible<optional<X>, const optional<Y>&>::value));
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>());

// TODO: Enable once P3068R6 is implemented
#if TEST_STD_VER >= 26 && 0
test_throwing();
#endif

return true;
}

TEST_CONSTEXPR_CXX26 bool test_throwing() {
{
typedef Z T;
typedef int U;
Expand All @@ -108,7 +115,18 @@ int main(int, char**) {
test<T>(rhs, true);
}

static_assert(!(std::is_constructible<optional<X>, const optional<Y>&>::value), "");
return true;
}

int main(int, char**) {
test();
#if TEST_STD_VER >= 20
static_assert(test());
#endif

{
test_throwing();
}

return 0;
}
Loading
Loading