Re: can the member function be compared?

Tech-Archive recommends: Fix windows errors by optimizing your registry



Igor Tandetnik wrote:
Alex Blekhman <tkfx.REMOVE@xxxxxxxxx> wrote:
"Bill Gates" wrote:
for example, there is a class as below

struct Foo
{
void Func1()
{}
void Func2()
{}
};

As known, &Foo::Func1 != &Foo::Func2 is right, but
&Foo::Func1 < &Foo::Func2
is illegal. So std::map< void ( Foo::* )() > can not be
defined. Are there
some other ways to get the greater and less relationship
between two member
functions?
The "&Foo::Func1 < &Foo::Func2" expression is perfectly
legal if both `Func1' and `Func2' are within the same access
level block. I.e., if `Func1' and `Func2' are not separated
by "public", "protected" or "private" labels, then their
comparison is legal and valid.

Here's the quote from the Standard:

<quote>
5.9/2 Relational operators

If two pointers point to nonstatic 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 are not
separated by an `access-specifier' label (11.1) and provided
their class is not a union.
If two pointers point to nonstatic data members of the
same object separated by an `access-specifier' label (11.1)
the result is unspecified.
</quote>

Note that this paragraph talks about regular pointers to data members. It's not applicable to pointers-to-members. 5.9/1 is applicable though:

5.9/1 The operands shall have arithmetic, enumeration or pointer type.

Note further that, according to 3.9.2/1, pointer types and pointers-to-member types are different kinds of types.

If you are still not convinced, look at the stadnard definition for equality (==) operator:

5.10/1 The == (equal to) and the != (not equal to) operators have the same semantic restrictions, conversions, and result type as the relational operators...
5.10/2 In addition, pointers to members can be compared...

Bottom line - pointers-to-members cannot be compared for inequality.

It seems that the greater or less relationship can not be gotten...
So another question is like below,
/*
struct Foo
{
void Func1(){}
void Func2(){}
};
*/
void* ptr = &( &Foo::Func1 );
Is the above line legal? And what dose the ptr pointer point to?

Thanks!
.



Relevant Pages

  • Re: can the member function be compared?
    ... legal if both `Func1' and `Func2' are within the same access ... If two pointers point to nonstatic data members of the ... 5.9/1 The operands shall have arithmetic, enumeration or pointer type. ...
    (microsoft.public.vc.language)
  • Re: Something wrong in my program
    ... void* converts implicitly to any other pointer type. ... That's not true in the case of function pointers. ...
    (comp.lang.c)
  • Re: sizeof(ptr) = ?
    ... A void * has the same representation as a char *. ... Char pointers which require additional data ... iff the default offset is 0. ...
    (comp.lang.c)
  • Re: Malloc code
    ... int xxx; ... As for not using the void pointer, I will have to do some further testing ... I just needed some insight on passing arrays of pointers. ... struct MCB *r1; ...
    (microsoft.public.vc.language)
  • Re: polymorphic design approach
    ... > polymorphism. ... * 'Device' pointers are stored so as to allow polymorphic ... virtual void handleMessage ...
    (alt.comp.lang.learn.c-cpp)