Topic: Implicit scope for pointer-to-member-functions


Author: "Andrew Koenig" <ark@acm.org>
Date: Sat, 23 May 2009 11:02:13 CST
Raw View
"Michael Mol" <mikemol@gmail.com> wrote in message
news:fd2cf8a5-c6c7-466b-9d06-535ef6a549c2@f19g2000yqh.googlegroups.com...

> Is there anything in the C++ standard (either the 98 version or 0x)
> that allows or disallows specifying a pointer-to-member-function in
> this form?

> class A
> {
> public:
>   void MyFirstFunc();
>   void MySecondFunc();;
> };

> void A::MySecondFunc()
> {
>   CallSomeFunc(&MyFirstFunc);
> }

MyFirstFunc and MySecondFunc are both members of A.  So when the body of
MySecondFunc contains

     CallSomeFunc(&MyFirstFunc);

that expression means the same as

     CallSomeFunc(&this->MyFirstFunc);

which isn't exactly what you had in mind.


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Michael Mol <mikemol@gmail.com>
Date: Thu, 26 Mar 2009 21:43:00 CST
Raw View
Is there anything in the C++ standard (either the 98 version or 0x)
that allows or disallows specifying a pointer-to-member-function in
this form?

class A
{
public:
   void MyFirstFunc();
   void MySecondFunc();;
};

void A::MySecondFunc()
{
   CallSomeFunc(&MyFirstFunc);
}

With the compiler I'm currently using, I have to explicitly identify
MyFirstFunc as in...

void A::MySecondFunc()
{
   CallSomeFunc(&A::MyFirstFunc);
}

I don't have the function scope rules memorized, but it would seem
logical to me that the A:: ought to be implicit in MySecondFunc's
body, as both MySecondFunc and MyFirstFunc are members of the same
class.

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: James Kanze <james.kanze@gmail.com>
Date: Fri, 27 Mar 2009 15:39:08 CST
Raw View
On Mar 27, 4:43 am, Michael  Mol <mike...@gmail.com> wrote:
> Is there anything in the C++ standard (either the 98 version
> or 0x) that allows or disallows specifying a
> pointer-to-member-function in this form?

> class A
> {
> public:
>    void MyFirstFunc();
>    void MySecondFunc();;
> };

> void A::MySecondFunc()
> {
>    CallSomeFunc(&MyFirstFunc);
> }

> With the compiler I'm currently using, I have to explicitly
> identify MyFirstFunc as in...

With any C++ compiler, you have to use a qualified identifier in
order to obtain a pointer to member.     5.3.1/2: "The result of
the unary & operator is a pointer to its operand. The operand
shall be an lvalue or a qualified-id."  And in the next
paragraph "A pointer to member is only formed when an explicit &
is used and its operand is a qualified-id not enclosed in
parentheses."

> void A::MySecondFunc()
> {
>    CallSomeFunc(&A::MyFirstFunc);
> }

> I don't have the function scope rules memorized, but it would
> seem logical to me that the A:: ought to be implicit in
> MySecondFunc's body, as both MySecondFunc and MyFirstFunc are
> members of the same class.

It has nothing to do with scope.  The '&' can result in either a
pointer, or a pointer to member.  Given something like:

     class C
     {
         int i ;
         void f() ;
     } ;

     void
     f()
     {
         int* p = &i ;           // int* designating the i in this
                                 // instance.
         int C::* pm = &C::i ;   // pointer to member designating
                                 // the i member of any instance.
         int* p2 = &(C::i) ;     // identical to the first (but
                                 // would be useful if i were, in
                                 // fact, a member of a base
                                 // class)
     }

In the case of functions, of course, the ability to obtain
either a pointer to the member object of this instance or a
pointer to member is irrelevant; functions can never be members
of a single instance.  But there doesn't seem to be any reason
to create a special case for functions, so they follow the same
rules as member data (and &f or &(C::f) are illegal).

--
James Kanze (GABI Software)             email:james.kanze@gmail.com
Conseils en informatique orient   e objet/
                    Beratung in objektorientierter Datenverarbeitung
9 place S   mard, 78210 St.-Cyr-l'   cole, France, +33 (0)1 30 23 00 34


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: wasti.redl@gmx.net
Date: Fri, 27 Mar 2009 17:59:51 CST
Raw View
On Mar 27, 4:43 am, Michael  Mol <mike...@gmail.com> wrote:
> Is there anything in the C++ standard (either the 98 version or 0x)
> that allows or disallows specifying a pointer-to-member-function in
> this form?
>
> class A
> {
> public:
>    void MyFirstFunc();
>    void MySecondFunc();;
>
> };
>
> void A::MySecondFunc()
> {
>    CallSomeFunc(&MyFirstFunc);
>
> }
>

C++03 5.3.1p3: "A pointer to member is only formed when an explicit &
is used and its operand is a qualified-id not enclosed in parentheses.
[Note: [...] Nor is &unqualified-id a pointer to member, even within
the scope of the unqualified-id's class.]"

Very explicitly disallowed. I don't know the rationale. When I
implemented pointers to members in Clang, it was not something that
made implementation easier - quite the opposite, in fact.


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]