I'm working on introspection technics and wanted to implement a trait to tell if a member-function is static or not.
After a bit of reading (so and cppreference) I understand that the type of a pointer to static member function is exactly the same as a the one of a pointer to free function with the same signature.
As a consequence I cannot:
- tell if a member function is
static; - retrieve the class from a pointer to
staticmember function; - tell if a class has a given
staticmember function (I saw this question but I'm not sure if it's the same goal and the answer relies onstd::experimentalthat I cannot use).
Is there really no way to do that? (I think that there isn't, at least for the two first dashes; for the third one, I'm less sure)
[EDIT] It will be clearer with some snippet:
struct S {
static int foo(int);
} template <typename Fun>
Process(Fun fun) {
std::cout << PrettyTypePrinting<is_member_of_t<FunPtr>>::Disp() << '\n';
}
int main() {
// tell if the input is a static member function
std::cout << std::boolalpha << is_static_member_v<decltype(S::foo)> << '\n';
// tell if the input has a given static member function
std::cout << std::boolalpha << has_static_foo_v<S> << '\n';
// retrieve the class from a member function
Process(S::foo);
}
Expecting:
true
true
struct S
It's pseudo-code by I hope it makes the goal clearer.
Besides, If there is a way to do that, I'd like to see the different solution for the different langage standard, starting with C++14.
Thanks
With c++20 concepts, you might be able to at least check if a member is static, if you know the name of the function:
Eigther will be fulfilled, if a "Function" member function exists, eigther static or non-static respectively. If we are talking about function-pointers, then things get significantly more difficult. I don't think there is any way to tell a function-pointer between static-member and free function apart at all; best you can do would be to wrap the creation of the function-pointer in some sort of macro that handles it. But we'd need to know your actual use-case to make any further recommendation.
You might need to modify and combine the concepts, since a static member-function can also be called from an object-reference. For arguments, you might need to add a variadic-template arg-pack to the concept; and for specific return-values you might need to add a return-type deduction check.