|
Boost Users : |
Subject: Re: [Boost-users] Annoying coding
From: Robert Jones (robertgbjones_at_[hidden])
Date: 2009-01-20 10:33:16
On Tue, Jan 20, 2009 at 3:22 PM, Zachary Turner <divisortheory_at_[hidden]>wrote:
> On Tue, Jan 20, 2009 at 9:11 AM, Robert Jones <robertgbjones_at_[hidden]>wrote:
>
>> On Tue, Jan 20, 2009 at 2:53 PM, Maxim Koshelev <chuchelo_at_[hidden]>wrote:
>>
>>> > The Lambda version would be similar, just replacing the outer bind with
>>> > infix notation
>>> >
>>> > #include "boost/lambda/lambda.hpp"
>>> > #include "boost/lambda/bind.hpp"
>>> >
>>> > using namespace boost::lambda;
>>> >
>>> > std::accumulate( begin, end, _1 + bind(&A::a, _2 ));
>>> >
>>> > Again untested.
>>>
>>> this std::accumulate return complicated lambda function, but not int
>>> value :-)
>>>
>>>
>> Yeah, sorry, missed the initial value!
>>
>> std::accumulate( begin, end, 0, _1 + bind(&A::a, _2 ));
>>
> I still get problems. Mostly ambiguity issues with _1 and _2, so I qualify
> them with boost::lambda and then I get
>
> boost::lambda::function_adaptor<Func>::apply none of the 8 overloads could
> convert all the argument types
> with
> [
> Func = int Test::*
> ]
> could be ...
>
> and from there it lists every possible overload of RET
> boost::lambda::function_adaptor<Func>::apply<RET>(T Test::*, _____)
>
> where ____ is every possible combination of const, volatile, pointer, and
> reference, and the type "Object".
>
>
This one IS tested!
#include "boost/lambda/lambda.hpp"
#include "boost/lambda/bind.hpp"
#include <vector>
#include <numeric>
#include <iostream>
struct A{
int a;
std::string b;
};
int a_sum(int i, const A &v){ return i + v.a; }
int main( )
{
using namespace boost::lambda;
std::vector<A> v;
for ( unsigned i=1; i != 6; ++i)
{
A a = { i, "" };
v.push_back( a );
}
int result = std::accumulate(v.begin(),v.end(),0, _1 + bind(&A::a,_2));
std::cout << result << "\n";
}
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