From 76347a14801247112a34b043d9afd12d225751d5 Mon Sep 17 00:00:00 2001 From: Casey Carter Date: Sat, 4 Feb 2023 21:26:27 -0800 Subject: [PATCH 1/2] Use `if constexpr` dispatch for `_Pass_fn` --- stl/inc/xutility | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/stl/inc/xutility b/stl/inc/xutility index 235fd627b38..95cdb60ba44 100644 --- a/stl/inc/xutility +++ b/stl/inc/xutility @@ -360,7 +360,6 @@ _EXPORT_STD struct identity { }; #endif // _HAS_CXX20 -// TRANSITION, VSO-386225 template struct _Ref_fn { // pass function object by value as a reference template @@ -376,17 +375,16 @@ struct _Ref_fn { // pass function object by value as a reference }; template -_INLINE_VAR constexpr bool _Pass_functor_by_value_v = conjunction_v, +_INLINE_VAR constexpr bool _Pass_functor_by_value = conjunction_v, is_trivially_copy_constructible<_Fn>, is_trivially_destructible<_Fn>>; -template , int> = 0> // TRANSITION, if constexpr -constexpr _Fn _Pass_fn(_Fn _Val) { // pass functor by value - return _Val; -} - -template , int> = 0> -constexpr _Ref_fn<_Fn> _Pass_fn(_Fn& _Val) { // pass functor by "reference" - return {_Val}; +template +constexpr auto _Pass_fn(_Fn& _Func) { + if constexpr (_Pass_functor_by_value<_Fn>) { + return _Func; // pass functor by value + } else { + return _Ref_fn<_Fn>{_Func}; // pass functor by "reference" + } } #if _HAS_CXX23 From 28b56980f79a7dae974b5bf0a26dc94f695541c0 Mon Sep 17 00:00:00 2001 From: Casey Carter Date: Sun, 5 Feb 2023 17:11:42 -0800 Subject: [PATCH 2/2] miscco's review comments --- stl/inc/xutility | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/stl/inc/xutility b/stl/inc/xutility index 95cdb60ba44..9e613dc1b70 100644 --- a/stl/inc/xutility +++ b/stl/inc/xutility @@ -375,13 +375,11 @@ struct _Ref_fn { // pass function object by value as a reference }; template -_INLINE_VAR constexpr bool _Pass_functor_by_value = conjunction_v, - is_trivially_copy_constructible<_Fn>, is_trivially_destructible<_Fn>>; - -template -constexpr auto _Pass_fn(_Fn& _Func) { - if constexpr (_Pass_functor_by_value<_Fn>) { - return _Func; // pass functor by value +constexpr auto _Pass_fn(_Fn& _Func) noexcept { + constexpr bool _Pass_by_value = conjunction_v, + is_trivially_copy_constructible<_Fn>, is_trivially_destructible<_Fn>>; + if constexpr (_Pass_by_value) { + return _Func; } else { return _Ref_fn<_Fn>{_Func}; // pass functor by "reference" }