#How do I check for constness when using member function pointers?

44 messages · Page 1 of 1 (latest)

astral thorn
thin veldtBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

astral thorn
#

C++14 only pls

raven roost
#

i don't quite understand what the purpose of this contraption would be

#

but apart from that

#

why would it not be valid to call a non-const member function of a Demo on a non-const Demo?

#

and ofc you can't call a non-const member function of a Demo on a const Demo

#

so if you ask me, looks like you just screwed up the asserts there?

#

note: build yourself something like std::void_t and you can avoid those helpers

#

also, be advised that just because calling a member function pointer on a given object is valid doesn't mean the class actually has a member function of a matching signature

#

so this thing really doesn't check whatever the name would suggest it's supposed to be checking

astral thorn
#

!f

thin veldtBOT
#

#include <iostream>
#include <type_traits>

template<typename ThisType, typename MemberFuncType, typename... Args>
std::true_type has_member_function_helper(
    decltype((std::declval<ThisType>().*std::declval<MemberFuncType>())(
        std::declval<Args>()...))* returnParam = nullptr);

template<typename ThisType, typename MemberFuncType, typename... Args>
std::false_type has_member_function_helper(...);

template<typename ThisType, typename MemberFuncType, typename... Args>
struct has_member_function
    : decltype(has_member_function_helper<ThisType, MemberFuncType, Args...>(
          nullptr)) {};

class Demo {
 public:
  int stuff(int a, int b) const { return a + b; }

  int stuff2(int a, int b) { return a - b; }
};

int main() {
  using Func1Type = int (Demo::*)(int, int) const;
  using Func2Type = int (Demo::*)(int, int);

  static_assert(has_member_function<Demo, Func2Type, int, int>::value,
                "Error 1");
  static_assert(
      has_member_function<const Demo, Func2Type, int, int>::value == false,
      "Error 2");

  static_assert(has_member_function<Demo, Func1Type, int, int>::value,
                "Error 3");
  static_assert(has_member_function<const Demo, Func1Type, int, int>::value,
                "Error 4");

  return 0;
}

Kolio98
raven roost
#

yeah

#

now change Demo to

class Demo {};
```and watch it compile just fine.
raven roost
#

just to make sure that's what you intended

#

because the name would suggest that's not what you intended

#

your has_member_function doesn't check whether the thing has a member function of that signature

astral thorn
#

wtf

raven roost
#

all it checks is whether it's possible to invoke a member function through a member function pointer

raven roost
raven roost
#

you don't

astral thorn
#

wut

raven roost
#

there's no way to check whether a given class has any member function of a given signature

#

at least not as far as i'd be aware

raven roost
#

you would need to know the name of the member function

#

at which point, might as well just make it an operator () and use one of the invocable traits or smth…

raven roost
#

ah those were added in 17

#

well anyways, just make it an operator () and check whether you can (args...) an object of the given type

#

ofc that also doesn't check for an exact signature

#

if you wanted to check that, i guess you could try to see if you can convert &class::fun to a member function pointer of the given signature

#

not 100% sure if that will work in the face of overloading

#

but there's an exception there to make it work for function pointers, i'd assume that there's a smilar rule for member function pointers

#

apart from all that: if you ask me, just don't.

#

nothing good has ever come off doing this sort of stuff

astral thorn
#

I wanted to learn about the limitations here

#

not use this in real life

#

!solved