Index: include/optional =================================================================== --- include/optional +++ include/optional @@ -436,46 +436,122 @@ } }; -template ::value> -struct __optional_storage; - -template -struct __optional_storage<_Tp, true> : __optional_storage_base<_Tp> +template ::value> +struct __optional_copy_base : __optional_storage_base<_Tp> { using __optional_storage_base<_Tp>::__optional_storage_base; }; template -struct __optional_storage<_Tp, false> : __optional_storage_base<_Tp> +struct __optional_copy_base<_Tp, false> : __optional_storage_base<_Tp> { - using value_type = _Tp; using __optional_storage_base<_Tp>::__optional_storage_base; _LIBCPP_INLINE_VISIBILITY - __optional_storage() = default; + __optional_copy_base() = default; _LIBCPP_INLINE_VISIBILITY - __optional_storage(const __optional_storage& __opt) + __optional_copy_base(const __optional_copy_base& __opt) { this->__construct_from(__opt); } _LIBCPP_INLINE_VISIBILITY - __optional_storage(__optional_storage&& __opt) + __optional_copy_base(__optional_copy_base&&) = default; + _LIBCPP_INLINE_VISIBILITY + __optional_copy_base& operator=(const __optional_copy_base&) = default; + _LIBCPP_INLINE_VISIBILITY + __optional_copy_base& operator=(__optional_copy_base&&) = default; +}; + +template ::value> +struct __optional_move_base : __optional_copy_base<_Tp> +{ + using __optional_copy_base<_Tp>::__optional_copy_base; +}; + +template +struct __optional_move_base<_Tp, false> : __optional_copy_base<_Tp> +{ + using value_type = _Tp; + using __optional_copy_base<_Tp>::__optional_copy_base; + + _LIBCPP_INLINE_VISIBILITY + __optional_move_base() = default; + _LIBCPP_INLINE_VISIBILITY + __optional_move_base(const __optional_move_base&) = default; + + _LIBCPP_INLINE_VISIBILITY + __optional_move_base(__optional_move_base&& __opt) noexcept(is_nothrow_move_constructible_v) { this->__construct_from(_VSTD::move(__opt)); } _LIBCPP_INLINE_VISIBILITY - __optional_storage& operator=(const __optional_storage& __opt) + __optional_move_base& operator=(const __optional_move_base&) = default; + _LIBCPP_INLINE_VISIBILITY + __optional_move_base& operator=(__optional_move_base&&) = default; +}; + +template ::value && + is_trivially_copy_constructible<_Tp>::value && + is_trivially_copy_assignable<_Tp>::value> +struct __optional_copy_assign_base : __optional_move_base<_Tp> +{ + using __optional_move_base<_Tp>::__optional_move_base; +}; + +template +struct __optional_copy_assign_base<_Tp, false> : __optional_move_base<_Tp> +{ + using __optional_move_base<_Tp>::__optional_move_base; + + _LIBCPP_INLINE_VISIBILITY + __optional_copy_assign_base() = default; + _LIBCPP_INLINE_VISIBILITY + __optional_copy_assign_base(const __optional_copy_assign_base&) = default; + _LIBCPP_INLINE_VISIBILITY + __optional_copy_assign_base(__optional_copy_assign_base&&) = default; + + _LIBCPP_INLINE_VISIBILITY + __optional_copy_assign_base& operator=(const __optional_copy_assign_base& __opt) { this->__assign_from(__opt); return *this; } _LIBCPP_INLINE_VISIBILITY - __optional_storage& operator=(__optional_storage&& __opt) + __optional_copy_assign_base& operator=(__optional_copy_assign_base&&) = default; +}; + +template ::value && + is_trivially_move_constructible<_Tp>::value && + is_trivially_move_assignable<_Tp>::value> +struct __optional_move_assign_base : __optional_copy_assign_base<_Tp> +{ + using __optional_copy_assign_base<_Tp>::__optional_copy_assign_base; +}; + +template +struct __optional_move_assign_base<_Tp, false> : __optional_copy_assign_base<_Tp> +{ + using value_type = _Tp; + using __optional_copy_assign_base<_Tp>::__optional_copy_assign_base; + + _LIBCPP_INLINE_VISIBILITY + __optional_move_assign_base() = default; + _LIBCPP_INLINE_VISIBILITY + __optional_move_assign_base(const __optional_move_assign_base& __opt) = default; + _LIBCPP_INLINE_VISIBILITY + __optional_move_assign_base(__optional_move_assign_base&&) = default; + _LIBCPP_INLINE_VISIBILITY + __optional_move_assign_base& operator=(const __optional_move_assign_base&) = default; + + _LIBCPP_INLINE_VISIBILITY + __optional_move_assign_base& operator=(__optional_move_assign_base&& __opt) noexcept(is_nothrow_move_assignable_v && is_nothrow_move_constructible_v) { @@ -498,11 +574,11 @@ template class optional - : private __optional_storage<_Tp> + : private __optional_move_assign_base<_Tp> , private __optional_sfinae_ctor_base_t<_Tp> , private __optional_sfinae_assign_base_t<_Tp> { - using __base = __optional_storage<_Tp>; + using __base = __optional_move_assign_base<_Tp>; public: using value_type = _Tp; Index: test/std/utilities/optional/optional.object/optional.object.assign/copy.pass.cpp =================================================================== --- test/std/utilities/optional/optional.object/optional.object.assign/copy.pass.cpp +++ test/std/utilities/optional/optional.object/optional.object.assign/copy.pass.cpp @@ -49,6 +49,24 @@ return lhs.has_value() && rhs.has_value() && *lhs == *rhs; } + +template +constexpr bool triviality_test = + std::is_trivially_copy_assignable>::value == + (std::is_trivially_destructible::value && + std::is_trivially_copy_constructible::value && + std::is_trivially_copy_assignable::value); + +void test_triviality_extension() { +#if defined(_LIBCPP_VER) || defined(_MSVC_STL_VER) + static_assert(triviality_test, ""); + static_assert(triviality_test>, ""); + static_assert(triviality_test, ""); + static_assert(triviality_test, ""); + static_assert(triviality_test, ""); +#endif +} + int main() { { @@ -99,4 +117,6 @@ } } #endif + + test_triviality_extension(); } Index: test/std/utilities/optional/optional.object/optional.object.assign/move.pass.cpp =================================================================== --- test/std/utilities/optional/optional.object/optional.object.assign/move.pass.cpp +++ test/std/utilities/optional/optional.object/optional.object.assign/move.pass.cpp @@ -51,6 +51,24 @@ bool X::throw_now = false; int X::alive = 0; +template +constexpr bool triviality_test = + std::is_trivially_move_assignable>::value == + (std::is_trivially_destructible::value && + std::is_trivially_move_constructible::value && + std::is_trivially_move_assignable::value); + +void test_triviality_extension() { +#if defined(_LIBCPP_VER) || defined(_MSVC_STL_VER) + static_assert(triviality_test, ""); + static_assert(triviality_test>, ""); + static_assert(triviality_test, ""); + static_assert(triviality_test, ""); + static_assert(triviality_test, ""); + static_assert(triviality_test, ""); +#endif +} + int main() { { @@ -147,28 +165,30 @@ } { 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; } + 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>::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; } + 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>::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; } + 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>::value, ""); } + + test_triviality_extension(); } Index: test/std/utilities/optional/optional.object/optional.object.ctor/copy.pass.cpp =================================================================== --- test/std/utilities/optional/optional.object/optional.object.ctor/copy.pass.cpp +++ test/std/utilities/optional/optional.object/optional.object.ctor/copy.pass.cpp @@ -10,7 +10,9 @@ // UNSUPPORTED: c++98, c++03, c++11, c++14 // -// optional(const optional& rhs); +// constexpr optional(const optional& rhs); +// If is_trivially_copy_constructible_v is true, +// this constructor shall be a constexpr constructor. #include #include @@ -35,10 +37,10 @@ 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; + Z() : count(0) {} + Z(Z const& o) : count(o.count + 1) + { if (count == 2) throw 6; } + int count; }; const Z z; const optional rhs(z); @@ -104,6 +106,48 @@ #endif } +constexpr bool test_constexpr() +{ + { + using T = int; + optional o1{}; + optional o2 = o1; + static_cast(o2); + optional o3{T{42}}; + optional o4 = o3; + static_cast(o4); + } + { + struct T { + constexpr T(int) {} + }; + optional o1{}; + optional o2 = o1; + static_cast(o2); + optional o3{T{42}}; + optional o4 = o3; + static_cast(o4); + } + return true; +} +static_assert(test_constexpr(), ""); + +template +constexpr bool triviality_test = + std::is_trivially_copy_constructible>::value == + std::is_trivially_copy_constructible::value; + +void test_triviality_extension() { +#if defined(_LIBCPP_VER) || defined(_MSVC_STL_VER) + static_assert(triviality_test, ""); + static_assert(triviality_test>, ""); + static_assert(triviality_test, ""); + static_assert(triviality_test, ""); + static_assert(triviality_test, ""); + static_assert(triviality_test, ""); +#endif +} + int main() { test(); @@ -146,10 +190,7 @@ test(); test(42); } - { - test_throwing_ctor(); - } - { - test_reference_extension(); - } + test_throwing_ctor(); + test_reference_extension(); + test_triviality_extension(); } Index: test/std/utilities/optional/optional.object/optional.object.ctor/move.pass.cpp =================================================================== --- test/std/utilities/optional/optional.object/optional.object.ctor/move.pass.cpp +++ test/std/utilities/optional/optional.object/optional.object.ctor/move.pass.cpp @@ -10,7 +10,10 @@ // UNSUPPORTED: c++98, c++03, c++11, c++14 // -// optional(optional&& rhs); +// constexpr optional(optional&& rhs); +// If is_trivially_move_constructible_v is true, +// this constructor shall be a constexpr constructor. + #include #include @@ -36,10 +39,10 @@ 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() : count(0) {} + Z(Z&& o) : count(o.count + 1) + { if (count == 2) throw 6; } + int count; }; Z z; optional rhs(std::move(z)); @@ -131,6 +134,48 @@ #endif } +constexpr bool test_constexpr() +{ + { + using T = int; + optional o1{}; + optional o2 = std::move(o1); + static_cast(o2); + optional o3{T{42}}; + optional o4 = std::move(o3); + static_cast(o4); + } + { + struct T { + constexpr T(int) {} + T(T&&) = default; + }; + optional o1{}; + optional o2 = std::move(o1); + static_cast(o2); + optional o3{T{42}}; + optional o4 = std::move(o3); + static_cast(o4); + } + return true; +} +static_assert(test_constexpr(), ""); + +template +constexpr bool triviality_test = + std::is_trivially_move_constructible>::value == + std::is_trivially_move_constructible::value; + +void test_triviality_extension() { +#if defined(_LIBCPP_VER) || defined(_MSVC_STL_VER) + static_assert(triviality_test, ""); + static_assert(triviality_test>, ""); + static_assert(triviality_test, ""); + static_assert(triviality_test, ""); + static_assert(triviality_test, ""); + static_assert(triviality_test, ""); +#endif +} int main() { @@ -178,9 +223,9 @@ test(); test(42); } - { - test_throwing_ctor(); - } + + test_throwing_ctor(); + { struct ThrowsMove { ThrowsMove() noexcept(false) {} @@ -195,7 +240,7 @@ }; static_assert(std::is_nothrow_move_constructible>::value, ""); } - { - test_reference_extension(); - } + + test_reference_extension(); + test_triviality_extension(); } Index: test/support/msvc_stdlib_force_include.hpp =================================================================== --- test/support/msvc_stdlib_force_include.hpp +++ test/support/msvc_stdlib_force_include.hpp @@ -26,6 +26,11 @@ #error This header may not be used when targeting libc++ #endif +// Indicates that we are using the MSVC standard library. +#ifndef _MSVC_STL_VER +#define _MSVC_STL_VER 42 +#endif + struct AssertionDialogAvoider { AssertionDialogAvoider() { _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE);