But I still don't really get it. The answerer said:
Function pointers are not relationally comparable in C++. Equality comparisons are supported, except for situations when at least one of the pointers actually points to a virtual member function (in which case the result is unspecified).
So that means they can be compared, right? Here's my attempt:
After working with it for awhile, I still can't figure it out.
I would suggest an alternative route either way (even if this did work). I know there are ways to solve this via metaprogramming which has no runtime hit.
Yes, it will work provided that pointer_to_my_method and method will have public access control that they could be accessed in main. From the C++ Standard:
"— If two pointers point to non-static data members of the same object, or to subobjects or array elements
of such members, recursively, the pointer to the later declared member compares greater provided the
two members have the same access control (Clause 11) and provided their class is not a union"
Oh yeah I forgot about declaring it public (just started using classes instead of structures, not really sure why but I did it!). Thanks for clearing that up!
struct A
{
void foo( int ) const {}
void bar( int ) const {}
void baz( int ) {}
int foobar( int ) const { return 0 ; }
};
#include <iostream>
int main()
{
constauto p = &A::foo ;
// two pointers of the same type can be equality-compared
if( p == &A::foo ) std::cout << "equal\n" ; // equal
if( p != &A::bar ) std::cout << "not equal\n" ; // not equal
// for pointers to functions, less-than-comparison etc are not allowed
// if( p < &A::bar ) std::cout << "not equal\n" ; // *** error
// any pointer can be equality-compared with nullptr
if( p != nullptr ) std::cout << "not null\n" ; // not null
// any pointer can be converted to a bool
if(p) std::cout << "not null\n" ; // not null
// two pointers to functions with different types can't be be equality-compared
// p == &A::baz ; // *** error; pointers have different types
// p == &A::foobar ; // *** error; ; pointers have different types
}