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

Discussion in 'C++' started by Toby Bradshaw, Jun 1, 2006.

  1. 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.
     
    Toby Bradshaw, Jun 1, 2006
    #1
    1. Advertising

  2. Toby Bradshaw

    Guest

    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
     
    , Jun 1, 2006
    #2
    1. Advertising

  3. Toby Bradshaw

    Noah Roberts Guest

    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.
     
    Noah Roberts, Jun 1, 2006
    #3
  4. Toby Bradshaw

    Jeff Flinn Guest

    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
     
    Jeff Flinn, Jun 1, 2006
    #4
  5. 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
     
    Toby Bradshaw, Jun 2, 2006
    #5
  6. Toby Bradshaw

    Noah Roberts Guest

    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.
     
    Noah Roberts, Jun 2, 2006
    #6
  7. Toby Bradshaw

    Kai-Uwe Bux Guest

    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
     
    Kai-Uwe Bux, Jun 2, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Colin Caughie
    Replies:
    1
    Views:
    737
    Shooting
    Aug 29, 2006
  2. Replies:
    4
    Views:
    430
  3. Replies:
    0
    Views:
    598
  4. Barry Ding

    boost::lambda::bind

    Barry Ding, Jul 22, 2007, in forum: C++
    Replies:
    0
    Views:
    601
    Barry Ding
    Jul 22, 2007
  5. Replies:
    8
    Views:
    591
    James Kanze
    Nov 21, 2007
Loading...

Share This Page