-1

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 static member function;
  • tell if a class has a given static member function (I saw this question but I'm not sure if it's the same goal and the answer relies on std::experimental that 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

6
  • 1
    I dont understand how the first bullet follows from what you write before. That you cannot tell the difference between pointer to free function and pointer to static function does not imply that you cannot distinguish non-static from static method. At least no obviously Commented Jan 17, 2024 at 15:21
  • the question is : "Given a type T and a pointer to one of its member functions, can you tell whether the member function is static or not?" ? Commented Jan 17, 2024 at 15:23
  • 1
    What are you introspecting, source or binary? Since in the source it's right there in the declaration. From a binary... why does it matter to. It almost behaves like a free function (except that it can access static private members of said class) Commented Jan 17, 2024 at 15:32
  • Specify what should be the user input(s). Like class name, pointer to static function etc. Commented Jan 17, 2024 at 15:33

1 Answer 1

0

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:

template<typename ClassT>
concept HasStaticMemberFunction = requires { ClassT::Function();};
template<typename ClassT>
concept HasNonStaticMemberFunction = requires(ClassT& object) { object.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.

Sign up to request clarification or add additional context in comments.

6 Comments

also a static member can be called via object.Function();
@463035818_is_not_an_ai That is true, you might need a combination of both concepts to actually determine the full state. For non-static, the first concept would not be fulfilled, so with a composite concept you hould be able to tell which type of function it is.
Also what if the static member function has many arguments.
@user12002570 You would need to specifiy potentialy arguments yourself, but this can be solved by adding a variadic template to the concept.
@Juliean Yes, I am aware of that. That comment is mainly for OP so that they know/realize that args must also be specified(if any)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.