[Lambda] Using placeholders as array indices

I am trying to use boost::lambda's placeholders as array indices. What I tried boils down to this: <code> #include <vector> #include <boost/lambda/bind.hpp> #include <boost/lambda/lambda.hpp> int main(int argc, char * argv[]) { using namespace boost::lambda; std::vector<int> a(10,0); bind(static_cast<std::vector<int>::reference (std::vector<int>::*)(std::vector<int>::size_type)>(&std::vector<int>::operator[]), a, _1)(0) = 1; } </code> But I get the following error (Visual C++ 2008 Express Edition, boost 1.35.0): error C2665: 'boost::lambda::function_adaptor<Func>::apply' : none of the 2 overloads could convert all the argument types What part am I missing? Thanks, P.

I am trying to use boost::lambda's placeholders as array indices. What I tried boils down to this: [snip] std::vector<int> a(10,0); bind(static_cast<std::vector<int>::reference (std::vector<int>::*)(std::vector<int>::size_type)>(&std::vect or<int>::operator[]), a, _1)(0) = 1; }
</code>
But I get the following error (Visual C++ 2008 Express Edition, boost 1.35.0):
error C2665: 'boost::lambda::function_adaptor<Func>::apply' : none of the 2 overloads could convert all the argument types
What part am I missing?
I did not try to understand why what's written above does not work, but it seems to me that writing the lambda as this would produce the intended result: (var(a)[_1])(0) = 1; Éric Malenfant --------------------------------------------- There are only two kinds of programming languages: those people always bitch about and those nobody uses. - Bjarne Stroustrup

2008/11/5 Eric MALENFANT <Eric.Malenfant@sagem-interstar.com>
I did not try to understand why what's written above does not work, but it seems to me that writing the lambda as this would produce the intended result: (var(a)[_1])(0) = 1;
Indeed, thanks! I was just about to answer that this does not solve the general problem of using placeholders as indices as this assumes the array is a left-value. Then I tested and realized that the const version of my original code: int x = bind(static_cast<std::vector<int>::const_reference (std::vector<int>::*)(std::vector<int>::size_type) const>(&std::vector<int>::operator[]), a, _1)(0); actually compiles and works. If anyone is knowledgeable about this I'd glad to hear an explanation. Thanks, P.

AMDG user790 user790 wrote:
2008/11/5 Eric MALENFANT <Eric.Malenfant@sagem-interstar.com <mailto:Eric.Malenfant@sagem-interstar.com>>
I did not try to understand why what's written above does not work, but it seems to me that writing the lambda as this would produce the intended result: (var(a)[_1])(0) = 1;
Indeed, thanks!
I was just about to answer that this does not solve the general problem of using placeholders as indices as this assumes the array is a left-value.
constant(a)[_1]... In Christ, Steven Watanabe
participants (3)
-
Eric MALENFANT
-
Steven Watanabe
-
user790 user790