Can I compare static methods?

This was discussed here: http://stackoverflow.com/questions/1765431/c-comparing-member-function-pointers

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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// A.h
class A
{
  void method();

  typedef void (A::*p_method)();
  static const p_method pointer_to_my_method;
}

// A.cpp
typedef void (A::*p_method)();
const p_method pointer_to_my_method = &A::method;

// main.cpp
int main()
{
  if (pointer_to_my_method == &A::p_method)
  {
    // yay!
  }
}


Would this work?
Last edited on
closed account (S6k9GNh0)
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.
@ausairman
Would this work?


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"

Here is a correct code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
 
class A
{
public:
  void method() {}
  typedef void (A::*p_method)();
  static const p_method pointer_to_my_method;
};
 
const A::p_method A::pointer_to_my_method = &A::method;
 
// main.cpp
int main()
{
    if ( A::pointer_to_my_method == &A::method )
    {
        std::cout << "They are equal" << std::endl;        
    }
    
    return 0;
}



Last edited on
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!
Take into account that function method is not a static method.
> If two pointers point to non-static data members ...
> .. the pointer to the later declared member compares greater provided ...

Data members. Not functions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
struct A
{
    void foo( int ) const {}
    void bar( int ) const {}
    void baz( int ) {}
    int foobar( int ) const { return 0 ; }
};

#include <iostream>

int main()
{
    const auto 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
}
@JLBorges
Data members. Not functions.



Oh, I am sorry. I am not attentive today.
Topic archived. No new replies allowed.