Boost logo

Boost Users :

Subject: Re: [Boost-users] [bind] How to dereference _1 in a boost::bind expression
From: Peter Barker (newbarker_at_[hidden])
Date: 2008-10-02 07:48:02


On Thu, Oct 2, 2008 at 11:39 AM, Robert Jones <robertgbjones_at_[hidden]> wrote:
> On Thu, Oct 2, 2008 at 11:12 AM, Peter Barker <newbarker_at_[hidden]> wrote:
>> Hello,
>>
>> Here's an example of my problem:
>>
>> #include <algorithm>
>> #include <vector>
>>
>> #include <boost/bind.hpp>
>>
>> class TheClass
>> {
>> };
>>
>> void accept(TheClass& tc)
>> {
>> }
>>
>> int main()
>> {
>> std::vector<TheClass*> cont;
>>
>> // Compile error - how to dereference _1 ???
>> std::for_each(cont.begin(),cont.end(),boost::bind(&accept,_1));
>> }
>>
>> How can I dereference _1 so I can use the accept function?
>
> #include <algorithm>
> #include <vector>
>
> #include <boost/lambda/bind.hpp>
> #include <boost/lambda/lambda.hpp>
>
> class TheClass
> {
> };
>
> void accept(TheClass& tc)
> {
> }
>
> int main()
> {
> using namespace boost;
> using namespace boost::lambda;
>
> std::vector<TheClass*> cont;
>
> // Compile error - how to dereference _1 ???
> std::for_each(cont.begin(),cont.end(),bind(accept,*_1));
> }

Rob,

Thanks for the suggestion - my sample program was actually too
stripped down. TheClass is a base with pure virtual methods and your
suggestion gives me the "cannot instantiate abstract class" compiler
error. Here's a new sample program:

#include <algorithm>
#include <vector>

#include <boost/lambda/bind.hpp>
#include <boost/lambda/lambda.hpp>

class TheClass
{
public:
        virtual ~TheClass() {}
        virtual void pvm() = 0;
};

class DerivedClass : public TheClass
{
        void pvm() {}
};

void accept(TheClass& tc)
{
}

int main()
{
        std::vector<TheClass*> cont;

        using namespace boost::lambda;
        using boost::lambda::_1;

        // Compile error - cannot instantiate abstract class - how to
dereference _1 ???
        std::for_each(cont.begin(),cont.end(),bind(&accept,*_1));
}


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net