From 7b089c0095dcd66c86dcc1d8312b2c1612b4ae9e Mon Sep 17 00:00:00 2001 From: "A. Jiang" Date: Sat, 1 Jul 2023 14:58:44 +0800 Subject: [PATCH] Make `function::target` return null function pointers without cast --- stl/inc/functional | 12 ++++++++++-- .../Dev11_0535636_functional_overhaul/test.cpp | 15 +++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/stl/inc/functional b/stl/inc/functional index e0e6564285..c66eedfef9 100644 --- a/stl/inc/functional +++ b/stl/inc/functional @@ -1134,12 +1134,20 @@ public: template _NODISCARD _Fx* target() noexcept { - return reinterpret_cast<_Fx*>(const_cast(this->_Target(typeid(_Fx)))); + if constexpr (is_function_v<_Fx>) { + return nullptr; + } else { + return reinterpret_cast<_Fx*>(const_cast(this->_Target(typeid(_Fx)))); + } } template _NODISCARD const _Fx* target() const noexcept { - return reinterpret_cast(this->_Target(typeid(_Fx))); + if constexpr (is_function_v<_Fx>) { + return nullptr; + } else { + return reinterpret_cast(this->_Target(typeid(_Fx))); + } } #else // _HAS_STATIC_RTTI const type_info& target_type() const noexcept = delete; // requires static RTTI diff --git a/tests/std/tests/Dev11_0535636_functional_overhaul/test.cpp b/tests/std/tests/Dev11_0535636_functional_overhaul/test.cpp index a83150a9f3..51151f4a36 100644 --- a/tests/std/tests/Dev11_0535636_functional_overhaul/test.cpp +++ b/tests/std/tests/Dev11_0535636_functional_overhaul/test.cpp @@ -1615,6 +1615,11 @@ void test_function() { assert(f.target() == nullptr); assert(c.target() == nullptr); + assert(f.target() == nullptr); + assert(c.target() == nullptr); + assert(f.target() == nullptr); + assert(c.target() == nullptr); + f = triple; assert(f(1000) == 3000); assert(f.target_type() == typeid(int (*)(int))); @@ -1623,6 +1628,11 @@ void test_function() { assert(f.target() == nullptr); assert(c.target() == nullptr); + assert(f.target() == nullptr); + assert(c.target() == nullptr); + assert(f.target() == nullptr); + assert(c.target() == nullptr); + f = short_long; assert(f(1000) == 29); assert(f.target_type() == typeid(short (*)(long))); @@ -1630,6 +1640,11 @@ void test_function() { assert(c.target() == nullptr); assert(*f.target() == &short_long); assert(*c.target() == &short_long); + + assert(f.target() == nullptr); + assert(c.target() == nullptr); + assert(f.target() == nullptr); + assert(c.target() == nullptr); }