Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

<functional>: Make function::target return null function pointers without cast #3844

Merged
merged 1 commit into from
Jul 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions stl/inc/functional
Original file line number Diff line number Diff line change
Expand Up @@ -1134,12 +1134,20 @@ public:

template <class _Fx>
_NODISCARD _Fx* target() noexcept {
return reinterpret_cast<_Fx*>(const_cast<void*>(this->_Target(typeid(_Fx))));
if constexpr (is_function_v<_Fx>) {
return nullptr;
} else {
return reinterpret_cast<_Fx*>(const_cast<void*>(this->_Target(typeid(_Fx))));
}
}

template <class _Fx>
_NODISCARD const _Fx* target() const noexcept {
return reinterpret_cast<const _Fx*>(this->_Target(typeid(_Fx)));
if constexpr (is_function_v<_Fx>) {
return nullptr;
} else {
return reinterpret_cast<const _Fx*>(this->_Target(typeid(_Fx)));
}
}
#else // _HAS_STATIC_RTTI
const type_info& target_type() const noexcept = delete; // requires static RTTI
Expand Down
15 changes: 15 additions & 0 deletions tests/std/tests/Dev11_0535636_functional_overhaul/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1615,6 +1615,11 @@ void test_function() {
assert(f.target<short (*)(long)>() == nullptr);
assert(c.target<short (*)(long)>() == nullptr);

assert(f.target<int(int)>() == nullptr);
assert(c.target<int(int)>() == nullptr);
assert(f.target<short(long)>() == nullptr);
assert(c.target<short(long)>() == nullptr);

f = triple;
assert(f(1000) == 3000);
assert(f.target_type() == typeid(int (*)(int)));
Expand All @@ -1623,13 +1628,23 @@ void test_function() {
assert(f.target<short (*)(long)>() == nullptr);
assert(c.target<short (*)(long)>() == nullptr);

assert(f.target<int(int)>() == nullptr);
assert(c.target<int(int)>() == nullptr);
assert(f.target<short(long)>() == nullptr);
assert(c.target<short(long)>() == nullptr);

f = short_long;
assert(f(1000) == 29);
assert(f.target_type() == typeid(short (*)(long)));
assert(f.target<int (*)(int)>() == nullptr);
assert(c.target<int (*)(int)>() == nullptr);
assert(*f.target<short (*)(long)>() == &short_long);
assert(*c.target<short (*)(long)>() == &short_long);

assert(f.target<int(int)>() == nullptr);
assert(c.target<int(int)>() == nullptr);
assert(f.target<short(long)>() == nullptr);
assert(c.target<short(long)>() == nullptr);
}


Expand Down