Problems mixing boost::lambda::bind and boost::shared_ptr..

T

Toby Bradshaw

Hi,

Consider the following:

class A
{
public:
virtual bool foo() = 0;
};

class B : public A
{
public:
virtual bool foo() { return false; }
};

void fn()
{
std::list< A * > aList(10);
std::list< boost::shared_ptr<A> > aSharedList(10);

// This works fine..
std::count_if(
aList.begin(),
aList.end(),
boost::lambda::bind(A::foo, boost::lambda::_1)
);

// This doesn't compile..
std::count_if(
aSharedList.begin(),
aSharedList.end(),
boost::lambda::bind(A::foo, boost::lambda::_1)
);
}

--

Why doesn't the second case compile ? The only difference is that I'm
making the call through a shared_ptr instead of a naked one. If
shared_ptr semantics are (essentially) identical to the naked pointer
ones then this should surely work ? Is there some extra syntax required
in the shared_ptr case or does this simply not work ?

Thanks in advance,
 
B

boaz_sade

Toby said:
Hi,

Consider the following:

class A
{
public:
virtual bool foo() = 0;
};

class B : public A
{
public:
virtual bool foo() { return false; }
};

void fn()
{
std::list< A * > aList(10);
std::list< boost::shared_ptr<A> > aSharedList(10);

// This works fine..
std::count_if(
aList.begin(),
aList.end(),
boost::lambda::bind(A::foo, boost::lambda::_1)
);

// This doesn't compile..
std::count_if(
aSharedList.begin(),
aSharedList.end(),
boost::lambda::bind(A::foo, boost::lambda::_1)
);
}

--

Why doesn't the second case compile ? The only difference is that I'm
making the call through a shared_ptr instead of a naked one. If
shared_ptr semantics are (essentially) identical to the naked pointer
ones then this should surely work ? Is there some extra syntax required
in the shared_ptr case or does this simply not work ?

Thanks in advance,
With g++ the error is this (sorry that is very very long lines)
no matching function for call to `boost::lambda::function_adaptor<bool
(A::*)() const>::apply(bool (A::*const&)() const,
boost::shared_ptr<A>&)'
canidate are:
static Result boost::lambda::function_adaptor<Result (Object::*)()
const>::apply(Result (Object::*)() const, const Object&) [with RET =
bool, Object = A, Result = bool]

As you can see it trys to match pointer to member function with
reference to member function - with boost::lambda don't have - why
shared_ptr is passing this? I'm not sure (its gatting late :) )
Hope you can continue from here..
good luck
 
N

Noah Roberts

You know, these boost questions would be better answered in the boost
mailing list. Many here are not familiar with boost and don't
appreciate the off-topic nature of discussion about it. You'll get
better responses where boost is on topic and where there are people
more familiar with how to use it.
 
J

Jeff Flinn

See inline response

Toby said:
Hi,

Consider the following:

class A
{
public:
virtual bool foo() = 0;
};

class B : public A
{
public:
virtual bool foo() { return false; }
};

void fn()
{
std::list< A * > aList(10);
std::list< boost::shared_ptr<A> > aSharedList(10);

// This works fine..
std::count_if(
aList.begin(),
aList.end(),
boost::lambda::bind(A::foo, boost::lambda::_1)

boost::lambda::bind(&A::foo, boost::lambda::_1)
);

// This doesn't compile..
std::count_if(
aSharedList.begin(),
aSharedList.end(),
boost::lambda::bind(A::foo, boost::lambda::_1)

boost::lambda::bind(&A::foo, boost::lambda::_1)
);
}

--

Why doesn't the second case compile ? The only difference is that I'm
making the call through a shared_ptr instead of a naked one. If
shared_ptr semantics are (essentially) identical to the naked pointer
ones then this should surely work ? Is there some extra syntax
required in the shared_ptr case or does this simply not work ?

I haven't used lambda bind a lot, but boost bind requires the & as shown
above, although some compilers will let you get by without it. Other than
that, the code will result in undefined behaviour since your containers are
not pointing to valid instances.

Jeff Flinn
 
T

Toby Bradshaw

Noah said:
You know, these boost questions would be better answered in the boost
mailing list. Many here are not familiar with boost and don't
appreciate the off-topic nature of discussion about it. You'll get
better responses where boost is on topic and where there are people
more familiar with how to use it.

Many on here are not familiar with virtual base classes but I would be
happy to answer questions on them. Furthermore.. many of the concepts
contained within Boost are being considered for inclusion in C++0x.

Regards,
 
N

Noah Roberts

Toby said:
Many on here are not familiar with virtual base classes but I would be
happy to answer questions on them.

You don't understand the difference??

Furthermore.. many of the concepts
contained within Boost are being considered for inclusion in C++0x.

Actually very few are so far.
 
K

Kai-Uwe Bux

Noah said:
You don't understand the difference??

Please, don't confuse the critique of a reasoning with the critique of the
conclusion. You wrote "Many here are not familiar with boost and don't
appreciate the off-topic nature of discussion about it." as though the
former was a reason as to why boost is off-topic. The parody of your
statement just goes to show that the reasoning is not sound. Of course, it
still maybe the case that boost is off-topic; but that would need to be
argued differently.

[snip]


Best

Kai-Uwe Bux
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top