#How do I check for constness when using member function pointers?
44 messages · Page 1 of 1 (latest)
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.
C++14 only pls
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
nevermind I got them in reverse lmao
!f
#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;
}
wut
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
but here Demo does not have these member functions
wtf
all it checks is whether it's possible to invoke a member function through a member function pointer
yes. you are also not checking for presence of certain member functions anywhere.
so how do I check that
you don't
wut
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
ðŸ˜
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…
which traits?
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