//===----------------------------------------------------------------------===// // // 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 // // optional& operator=(optional&& rhs) // noexcept(is_nothrow_move_assignable::value && // is_nothrow_move_constructible::value); #include #include #include #include "test_macros.h" using std::experimental::optional; struct AllowConstAssign { AllowConstAssign(AllowConstAssign const&) {} AllowConstAssign const& operator=(AllowConstAssign const&) const { return *this; } }; struct X { static bool throw_now; X() = default; X(X&&) { if (throw_now) TEST_THROW(6); } X& operator=(X&&) noexcept { return *this; } }; bool X::throw_now = false; struct Y {}; int main() { { static_assert(std::is_nothrow_move_assignable>::value, ""); optional opt; constexpr optional opt2; opt = std::move(opt2); static_assert(static_cast(opt2) == false, ""); assert(static_cast(opt) == static_cast(opt2)); } { optional opt; constexpr optional opt2(2); opt = std::move(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 = std::move(opt2); static_assert(static_cast(opt2) == false, ""); assert(static_cast(opt) == static_cast(opt2)); } { optional opt(3); constexpr optional opt2(2); opt = std::move(opt2); static_assert(static_cast(opt2) == true, ""); static_assert(*opt2 == 2, ""); assert(static_cast(opt) == static_cast(opt2)); assert(*opt == *opt2); } { optional opt; optional opt2; opt = std::move(opt2); } #ifndef TEST_HAS_NO_EXCEPTIONS { static_assert(!std::is_nothrow_move_assignable>::value, ""); optional opt; optional opt2(X{}); assert(static_cast(opt2) == true); try { X::throw_now = true; opt = std::move(opt2); assert(false); } catch (int i) { assert(i == 6); assert(static_cast(opt) == false); } } #endif { static_assert(std::is_nothrow_move_assignable>::value, ""); } }