开发者

boost function and lambda to wrap a function

I want to convert this simple code:

void setZComp(Imath::V3f& pt)
{
    pt.z = 0.0;
}

int myfunc()
{
    ...

    std::vector<Imath::V3f> vec(5,Imath::V3f(1.0,1.0,1.0));
    std::for_each(vec.beg开发者_运维百科in(),vec.end(),boost::bind(&setZComp,_1));

    ...
}

to something like that, in order to not have setZComp declared outside but some sort of inline declaration

int myfunc()
{
    ...

    boost::function<double(Imath::V3f&)> f = (boost::lambda::_1 ->* &Imath::V3f::z = 0.0) ;
    std::for_each(vec.begin(),vec.end(),boost::bind(&f,_1));

    ...
}

I'm quite new to Boost Bind and Lambda and I don't know if this can be done in some way. Obviously the code above does not work.


Are you using a sledgehammer to break a nut? Sometimes, I think it is simpler to just use a normal for loop and set the variable explicitly yourself. This makes the code much easier to read and maintain.

typedef std::vector<Imath::V3f> V3fVector;
V3fVector vec(5,Imath::V3f(1.0,1.0,1.0));

for (V3fVector::iterator i = vec.begin(), iEnd = vec.end(); iEnd != i; ++i)
    i->z = 0.0;

As much as boost bind is useful, its also a syntactical mess that make simple code unreadable.


If you cannot use a C++11 lambda, then you can use boost::lambda::bind.
So in your case something like the following:

boost::lambda::bind(&Imath::V3f::z, boost::lambda::_1) = 0.0

A full example since I don't know your internals:

struct S
{
    S():i(0){};
    int i;
};
int main()
{
    std::vector<S> vec;
    vec.push_back(S());

    std::for_each(vec.begin(), vec.end(), boost::lambda::bind(&S::i, boost::lambda::_1) = 5);
    std::cout << vec.front().i << std::endl; // outputs 5
    return 0
}


You might also consider taking a look at boost::phoenix. I think it's a more fully fleshed out implementation of functional programming for c++ than the lambda library.


As explained in the section Member variables as targets:

A pointer to a member variable is not really a function, but the first argument to the [boost::lambda::bind] function can nevertheless be a pointer to a member variable. Invoking such a bind expression returns a reference to the data member.

So to construct a lambda expression that accesses the z member, you can use:

boost::lambda::bind(&Imath::V3f::z, boost::lambda::_1)

The returned object can itself be used in other expressions. For example,

boost::lambda::bind(&Imath::V3f::z, boost::lambda::_1) = 0.0

means "obtain the double ref to the z member of the first argument (type Imath::V3f&) and assign the value 0.0".

You can then use this lambda with Boost.Function and std::for_each:

boost::function<void(Imath::V3f&)> f = boost::lambda::bind(&Imath::V3f::z, boost::lambda::_1) = 0.0;
std::for_each(vec.begin(), vec.end(), f);

For reference, here is a complete, compilable example:

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <boost/function.hpp>
#include <boost/lambda/bind.hpp>
#include <boost/lambda/lambda.hpp>

namespace Imath
{
class V3f
{
public:
    double x, y, z;

    V3f(double x_, double y_, double z_)
        : x(x_), y(y_), z(z_)
    {
    }

    friend std::ostream& operator<<(std::ostream& os, const V3f& pt) {
        return (os << '(' << pt.x << ", " << pt.y << ", " << pt.z << ')');
    }
};
}

int main()
{
    std::vector<Imath::V3f> vec(5, Imath::V3f(1.0, 1.0, 1.0));
    boost::function<void(Imath::V3f&)> f = boost::lambda::bind(&Imath::V3f::z, boost::lambda::_1) = 0.0;
    std::for_each(vec.begin(), vec.end(), f);

    std::vector<Imath::V3f>::iterator it, end = vec.end();
    for (it = vec.begin(); it != end; ++it) {
        std::cout << *it << std::endl;
    }

    return EXIT_SUCCESS;
}

Outputs:

(1, 1, 0)
(1, 1, 0)
(1, 1, 0)
(1, 1, 0)
(1, 1, 0)


If you have access to a recent version of g++ with C++11 support, or MSVC 2010, you could do the following:

int myfunc()
{
    ...

    std::for_each(vec.begin(),vec.end(),[](Imath::V3f& pt){ pt.z = 0.0; });

    ...
}


If you want to use boost::lambda, I sometimes find it cleaner to declare a "pointer-to-member" variable immediately before the line that contains the lambda, which then allows you to use the ->* operator instead of using boost::lambda::bind.

However, as Alan pointed out, a simple loop here might be the simplest solution. Use BOOST_FOREACH to make it even simpler.

Here's a modified version of mkaes's sample implementation that uses operator ->* instead of bind, and it also shows how to use BOOST_FOREACH as an alternative.

#include <iostream>
#include <vector>
#include <boost/lambda/lambda.hpp>
#include <boost/foreach.hpp>

// I like to provide alternate names for the boost::lambda placeholders
boost::lambda::placeholder1_type& arg1 = boost::lambda::_1 ;
boost::lambda::placeholder2_type& arg2 = boost::lambda::_2 ;
boost::lambda::placeholder3_type& arg3 = boost::lambda::_3 ;

struct S
{
    S():i(0){};
    int i;
};

int main()
{
    std::vector<S> vec;
    vec.push_back(S());

    // Define this pointer-to-member so we can
    //  use it in the lambda via the ->* operator
    int S::* i = &S::i ;

    std::for_each(vec.begin(), vec.end(), &arg1->*i = 5);
    std::cout << vec.front().i << std::endl; // outputs 5

    // Alternatively, just use a simple foreach loop
    BOOST_FOREACH( S & s, vec )
    {
        s.i = 6 ;
    }
    std::cout << vec.front().i << std::endl; // outputs 6

    return 0 ;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜