Ah Kindred spirits. I asked a similar question awhile ago (like last year). I am sorry I was not able to find the original message.

 

The answer I was given was that the & is the correct syntax per the CPP standard. What you are doing is taking the address of a memberfunction.

 

What was confusing to me was that some compilers allow you to LEAVE OFF the & while others don’t. The ones that do are trying to be helpful but are not conformant.

 

 


From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Sean DeNigris
Sent: Tuesday, March 29, 2005 7:31 PM
To: boost-users@lists.boost.org
Subject: [Boost-users] Boost.lambda

 

Can anyone explain why the class member functions need their address taken when used in a boost::lambda::bind inside the class?

 

e.g.

vector<int> v;

MyClass theClass;

for_each(v.begin(), v.end(),

bind(MyClass::DoSomething, &theClass, _1));

 

//The above compiles fine, but…

 

class MyClass {

public:

            void DoSomething(const int& i) { … }

            void Go()

{

for_each(myInts.begin(), myInts.end(),

/*--------------------->*/         bind(MyClass::DoSomething, this, _1));

}

            vector<int> myInts;

};

 

The above code does not compile.  The marked line needs to be:

bind(&MyClass::DoSomething, this, _1));

Why?

Thanks.

            - Sean