Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Problems mixing boost::lambda::bind and boost::shared_ptr..

Reply
Thread Tools

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

 
 
Toby Bradshaw
Guest
Posts: n/a
 
      06-01-2006
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,

--
t o b e
Rawflow,
London, UK.
 
Reply With Quote
 
 
 
 
boaz_sade@yahoo.com
Guest
Posts: n/a
 
      06-01-2006

Toby Bradshaw wrote:
> 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,
>
> --
> t o b e
> Rawflow,
> London, UK.

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

 
Reply With Quote
 
 
 
 
Noah Roberts
Guest
Posts: n/a
 
      06-01-2006
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.

 
Reply With Quote
 
Jeff Flinn
Guest
Posts: n/a
 
      06-01-2006
See inline response

Toby Bradshaw wrote:
> 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


 
Reply With Quote
 
Toby Bradshaw
Guest
Posts: n/a
 
      06-02-2006
Noah Roberts wrote:
> 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,

--
t o b e
 
Reply With Quote
 
Noah Roberts
Guest
Posts: n/a
 
      06-02-2006

Toby Bradshaw wrote:
> Noah Roberts wrote:
> > 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.


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.

 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      06-02-2006
Noah Roberts wrote:

>
> Toby Bradshaw wrote:
>> Noah Roberts wrote:
>> > 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.

>
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Mixing and matching VB and C# in vs.net 2005 jason@cyberpine.com ASP .Net 3 09-30-2006 02:51 AM
Mixing Workgroup and Domain PC's Lee Wireless Networking 2 11-01-2004 09:04 PM
Mixing comb and reg part in one process valentin tihomirov VHDL 7 01-02-2004 05:03 PM
Problems mixing regular form fields with file uploads David Heinemeier Hansson Ruby 3 10-29-2003 08:43 AM
Optimisation problems when mixing asm with C++ Oliver Batchelor C++ 1 07-22-2003 08:18 AM



Advertisments