//===----------------------------------------------------------------------===// // // The LLVM Compiler Infrastructure // // This file is dual licensed under the MIT and the University of Illinois Open // Source Licenses. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// // UNSUPPORTED: c++98, c++03, c++11, c++14 // // From LWG2451: // template // optional& operator=(const optional& rhs); #include #include #include #include "test_macros.h" #include "archetypes.hpp" 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 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 int AssignableFrom::type_constructed = 0; template int AssignableFrom::type_assigned = 0; template int AssignableFrom::int_constructed = 0; template int AssignableFrom::int_assigned = 0; void test_with_test_type() { using T = TestTypes::TestType; T::reset(); { // non-empty to empty T::reset_constructors(); optional opt; const optional 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(other) == true); assert(*other == 42); assert(static_cast(opt) == true); assert(*opt == T(42)); } assert(T::alive == 0); { // non-empty to non-empty optional opt(101); const optional 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(other) == true); assert(*other == 42); assert(static_cast(opt) == true); assert(*opt == T(42)); } assert(T::alive == 0); { // empty to non-empty optional opt(101); const optional 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(other) == false); assert(static_cast(opt) == false); } assert(T::alive == 0); { // empty to empty optional opt; const optional 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(other) == false); assert(static_cast(opt) == false); } assert(T::alive == 0); } void test_ambigious_assign() { using OptInt = std::optional; { using T = AssignableFrom; const OptInt a(42); T::reset(); { std::optional 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(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(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 a(42); T::reset(); { std::optional 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; static_assert(!std::is_assignable_v, ""); } } } int main() { test_with_test_type(); test_ambigious_assign(); { optional opt; constexpr optional opt2; opt = opt2; static_assert(static_cast(opt2) == false, ""); assert(static_cast(opt) == static_cast(opt2)); } { optional opt; constexpr optional opt2(short{2}); opt = opt2; static_assert(static_cast(opt2) == true, ""); static_assert(*opt2 == 2, ""); assert(static_cast(opt) == static_cast(opt2)); assert(*opt == *opt2); } { optional opt(3); constexpr optional opt2; opt = opt2; static_assert(static_cast(opt2) == false, ""); assert(static_cast(opt) == static_cast(opt2)); } { optional opt(3); constexpr optional opt2(short{2}); opt = opt2; static_assert(static_cast(opt2) == true, ""); static_assert(*opt2 == 2, ""); assert(static_cast(opt) == static_cast(opt2)); assert(*opt == *opt2); } #ifndef TEST_HAS_NO_EXCEPTIONS { optional opt; optional opt2(42); assert(static_cast(opt2) == true); try { X::throw_now = true; opt = opt2; assert(false); } catch (int i) { assert(i == 6); assert(static_cast(opt) == false); } } #endif }