From 3f6d9393471350b4c8dcf591e3b5cd2397fd6dd3 Mon Sep 17 00:00:00 2001 From: "A. Jiang" Date: Wed, 5 Apr 2023 14:06:35 +0800 Subject: [PATCH 1/4] Don't include `` in `` and `` --- stl/inc/optional | 12 +++--------- stl/inc/variant | 14 ++++++-------- 2 files changed, 9 insertions(+), 17 deletions(-) diff --git a/stl/inc/optional b/stl/inc/optional index a9a4ee02945..1eed9e388b9 100644 --- a/stl/inc/optional +++ b/stl/inc/optional @@ -11,15 +11,9 @@ #if !_HAS_CXX17 _EMIT_STL_WARNING(STL4038, "The contents of are available only with C++17 or later."); #else // ^^^ !_HAS_CXX17 / _HAS_CXX17 vvv -#if _HAS_CXX20 -#include -#endif // _HAS_CXX20 #include -#include -#include -#include -#include #include +#include #pragma pack(push, _CRT_PACKING) #pragma warning(push, _STL_WARNING_LEVEL) @@ -104,7 +98,7 @@ struct _Optional_destruct_base<_Ty, false> { // either contains a value of _Ty o _CONSTEXPR20 ~_Optional_destruct_base() noexcept { if (_Has_value) { - _Destroy_in_place(_Value); + _Value.~decltype(_Value); } } @@ -129,7 +123,7 @@ struct _Optional_destruct_base<_Ty, false> { // either contains a value of _Ty o _CONSTEXPR20 void reset() noexcept { if (_Has_value) { - _Destroy_in_place(_Value); + _Value.~decltype(_Value); _Has_value = false; } } diff --git a/stl/inc/variant b/stl/inc/variant index 670c5b4af3e..e546d5a5e83 100644 --- a/stl/inc/variant +++ b/stl/inc/variant @@ -13,12 +13,8 @@ _EMIT_STL_WARNING(STL4038, "The contents of are available only with C++17 or later."); #else // ^^^ !_HAS_CXX17 / _HAS_CXX17 vvv #include -#include -#include -#include -#include #include -#include +#include #pragma pack(push, _CRT_PACKING) #pragma warning(push, _STL_WARNING_LEVEL) @@ -808,8 +804,9 @@ public: _CONSTEXPR20 void _Destroy() noexcept { // destroy the contained value // pre: _Idx == index() - if constexpr (_Idx != variant_npos && !is_trivially_destructible_v<_Meta_at_c, _Idx>>) { - _STD _Destroy_in_place(_STD _Variant_raw_get<_Idx>(_Storage())); + using _Indexed_value_type = remove_cv_t<_Meta_at_c, _Idx>>; + if constexpr (_Idx != variant_npos && !is_trivially_destructible_v<_Indexed_value_type>) { + _STD _Variant_raw_get<_Idx>(_Storage()).~_Indexed_value_type(); } } @@ -817,7 +814,8 @@ public: if constexpr (!conjunction_v...>) { _STD _Variant_raw_visit(index(), _Storage(), [](auto _Ref) noexcept { if constexpr (decltype(_Ref)::_Idx != variant_npos) { - _STD _Destroy_in_place(_Ref._Val); + using _Indexed_value_type = _Remove_cvref_t; + _Ref._Val.~_Indexed_value_type(); } }); } From 1e04d1268a1785273559786f0a0b34db79b029dc Mon Sep 17 00:00:00 2001 From: "A. Jiang" Date: Wed, 5 Apr 2023 14:45:13 +0800 Subject: [PATCH 2/4] Fix destructor calls --- stl/inc/optional | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stl/inc/optional b/stl/inc/optional index 1eed9e388b9..02d5f7ddbeb 100644 --- a/stl/inc/optional +++ b/stl/inc/optional @@ -98,7 +98,7 @@ struct _Optional_destruct_base<_Ty, false> { // either contains a value of _Ty o _CONSTEXPR20 ~_Optional_destruct_base() noexcept { if (_Has_value) { - _Value.~decltype(_Value); + _Value.~decltype(_Value)(); } } @@ -123,7 +123,7 @@ struct _Optional_destruct_base<_Ty, false> { // either contains a value of _Ty o _CONSTEXPR20 void reset() noexcept { if (_Has_value) { - _Value.~decltype(_Value); + _Value.~decltype(_Value)(); _Has_value = false; } } From 8647ef55bb7f4cbf648941afb79d15b3ff1c1f20 Mon Sep 17 00:00:00 2001 From: "A. Jiang" Date: Wed, 5 Apr 2023 15:15:36 +0800 Subject: [PATCH 3/4] Reduce `numeric_limits` dependency for `variant` --- stl/inc/variant | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/stl/inc/variant b/stl/inc/variant index e546d5a5e83..7e36f4128f1 100644 --- a/stl/inc/variant +++ b/stl/inc/variant @@ -689,10 +689,12 @@ _NODISCARD constexpr _Variant_raw_visit_t<_Fn, _Storage> _Variant_raw_visit( template class _Variant_base; +inline constexpr size_t _Schar_max_as_size = static_cast(-1) / 2; +inline constexpr size_t _Short_max_as_size = static_cast(-1) / 2; + template using _Variant_index_t = // signed so that conversion of -1 to size_t can cheaply sign extend - conditional_t<(_Count < static_cast((numeric_limits::max)())), signed char, - conditional_t<(_Count < static_cast((numeric_limits::max)())), short, int>>; + conditional_t<(_Count < _Schar_max_as_size), signed char, conditional_t<(_Count < _Short_max_as_size), short, int>>; template struct _Variant_construct_visitor { // visitor that constructs the same alternative in a target _Variant_base as is From 2fabe95776d5dc599bed98ad4a2f1c27f709e768 Mon Sep 17 00:00:00 2001 From: "A. Jiang" Date: Thu, 6 Apr 2023 07:16:15 +0800 Subject: [PATCH 4/4] Address @strega-nil-ms's review comments --- stl/inc/optional | 10 ++++++++-- stl/inc/variant | 6 ++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/stl/inc/optional b/stl/inc/optional index 02d5f7ddbeb..093cbb659b7 100644 --- a/stl/inc/optional +++ b/stl/inc/optional @@ -11,7 +11,13 @@ #if !_HAS_CXX17 _EMIT_STL_WARNING(STL4038, "The contents of are available only with C++17 or later."); #else // ^^^ !_HAS_CXX17 / _HAS_CXX17 vvv +#if _HAS_CXX20 +#include +#endif // _HAS_CXX20 #include +#include +#include +#include #include #include @@ -98,7 +104,7 @@ struct _Optional_destruct_base<_Ty, false> { // either contains a value of _Ty o _CONSTEXPR20 ~_Optional_destruct_base() noexcept { if (_Has_value) { - _Value.~decltype(_Value)(); + _Value.~_Ty(); } } @@ -123,7 +129,7 @@ struct _Optional_destruct_base<_Ty, false> { // either contains a value of _Ty o _CONSTEXPR20 void reset() noexcept { if (_Has_value) { - _Value.~decltype(_Value)(); + _Value.~_Ty(); _Has_value = false; } } diff --git a/stl/inc/variant b/stl/inc/variant index 7e36f4128f1..478de8c34ad 100644 --- a/stl/inc/variant +++ b/stl/inc/variant @@ -12,7 +12,13 @@ #if !_HAS_CXX17 _EMIT_STL_WARNING(STL4038, "The contents of are available only with C++17 or later."); #else // ^^^ !_HAS_CXX17 / _HAS_CXX17 vvv +#if _HAS_CXX20 +#include +#endif // _HAS_CXX20 #include +#include +#include +#include #include #include