From: Boris Bralo on 6 May 2010 05:29 > > This is not the correct calling syntax on a pointer to member function, > and I am confident that VC++ 2005 would not accept this. It should be: > > (o->*memfn)() You're right, but I never got to it. Main problem is why compiler deduces int(AA::*)() when I explicitly sent int(BB::*)(). -- Boris -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Nick Hounsome on 6 May 2010 05:28 On May 5, 2:35 am, Boris Bralo <boris.br...(a)gmail.com> wrote: > Hi all; > > Here's the code that doesn't compile on latest Comeau (online) and GCC > 4.4.0. It works with VC++ 2005 : > > template <typename T> > class A{ > public: > template <typename R> > void meth( T* o, R (T::*memfn)() ){ > o->memfn(); > > } > }; > > struct AA{ > int aaa(){ return 1; }; > > }; > > struct BB: public AA{ > > }; > > int main(int , char**){ > BB bb; > A<BB> a; > a.meth(&bb, &BB::aaa); > return 0; > > } > > Both GCC and Comeau try to instantiate A::meth(&bb, &AA::aaa) and > report error. Are they right? A subtle clarification of meaning: :: Is the scope resolution operator not the "member operator". Unlike some other languages BB::aaa does not mean "the aaa member of BB" - It means "The thing(s) called aaa that are visible in the scope of BB". This is usually clearer when dealing with namespaces than with classes but it is the same mechanism. There is actually no way to refer to "the thing(s) called aaa that are declared in AA". The soloution is to not restrict the class of the memfun to T and just leave the compiler to complain about the use implementation of meth rather than the call to it. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Boris Bralo on 7 May 2010 03:12
> :: Is the scope resolution operator not the "member operator". > > Unlike some other languages BB::aaa does not mean "the aaa member of > BB" - It means "The thing(s) called aaa that are visible in the scope > of BB". This is usually clearer when dealing with namespaces than with > classes but it is the same mechanism. > > There is actually no way to refer to "the thing(s) called aaa that are > declared in AA". > > The soloution is to not restrict the class of the memfun to T and just > leave the compiler to complain about the use implementation of meth > rather than the call to it. > Thank you all for clarifications. I've decided to go with template <typename R, typename Other> void meth( T* o, R (Other::*memfn)() ){ (o->*memfn)(); } --- Boris -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |