From: Jonathan Wakely Date: Mon, 28 Apr 2025 13:51:57 +0000 (+0100) Subject: libstdc++: Use constexpr-if in std::function for C++11 and C++14 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5b32ce7f7b9b8bd651654003e1be1b1aa75b6845;p=thirdparty%2Fgcc.git libstdc++: Use constexpr-if in std::function for C++11 and C++14 This allows removing the _Target_handler class template, because it's no longer needed to prevent instantiating invalid specializations of _Function_handler. libstdc++-v3/ChangeLog: * include/bits/std_function.h (_Target_handler): Remove. (function::target): Use constexpr-if for C++11 and C++14, with diagnostic pragmas to suppress warnings. Reviewed-by: Tomasz KamiƄski --- diff --git a/libstdc++-v3/include/bits/std_function.h b/libstdc++-v3/include/bits/std_function.h index 1bf8b9ad6e8..3bfbe824026 100644 --- a/libstdc++-v3/include/bits/std_function.h +++ b/libstdc++-v3/include/bits/std_function.h @@ -135,13 +135,16 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION static _Functor* _M_get_pointer(const _Any_data& __source) noexcept { - if _GLIBCXX17_CONSTEXPR (__stored_locally) +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wc++17-extensions" // if constexpr + if constexpr (__stored_locally) { const _Functor& __f = __source._M_access<_Functor>(); return const_cast<_Functor*>(std::__addressof(__f)); } else // have stored a pointer return __source._M_access<_Functor*>(); +#pragma GCC diagnostic pop } private: @@ -312,21 +315,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { return false; } }; - // Avoids instantiating ill-formed specializations of _Function_handler - // in std::function<_Signature>::target<_Functor>(). - // e.g. _Function_handler and _Function_handler - // would be ill-formed. - template::value> - struct _Target_handler - : _Function_handler<_Signature, typename remove_cv<_Functor>::type> - { }; - - template - struct _Target_handler<_Signature, _Functor, false> - : _Function_handler - { }; - /** * @brief Polymorphic function wrapper. * @ingroup functors @@ -644,13 +632,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION const _Functor* target() const noexcept { - if _GLIBCXX17_CONSTEXPR (is_object<_Functor>::value) +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wc++17-extensions" // if constexpr + if constexpr (is_object<_Functor>::value) { - // For C++11 and C++14 if-constexpr is not used above, so - // _Target_handler avoids ill-formed _Function_handler types. - using _Handler = _Target_handler<_Res(_ArgTypes...), _Functor>; - - if (_M_manager == &_Handler::_M_manager + if (_M_manager == &_Handler<_Functor>::_M_manager #if __cpp_rtti || (_M_manager && typeid(_Functor) == target_type()) #endif @@ -661,6 +647,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return __ptr._M_access(); } } +#pragma GCC diagnostic pop return nullptr; } /// @}