Peter
There's also std::transform( nums.begin(),nums.end(), nums.begin(), _1 + 1 );
Thank you, that is the answer which is better from the point of view of functional behaviour. I have also been able to do a binary operation as well:
std::transform(nums.begin(),nums.end(),nums.begin(),nums.begin(), (_1 + _2) );
I was ignorant of std::transform although I now see it has a reference on the page for std::for_each!
Thanks again
John
P.S. My email system does not make it easy to do quoting.
________________________________
From: Peter Dimov
Hi
I have been playing around with lambda2 to see what I can do.
I have an example from http://www.enseignement.polytechnique.fr/informatique/INF478/docs/Cpp/en/cpp... where the task is to increment an array of integers:
std::vector<int> nums{3, 4, 2, 9, 15, 267}; std::for_each(nums.begin(), nums.end(), [](int &n){ n++; }); The nearest I can get with lambda2 is this:
std::for_each( nums.begin(),nums.end(), (_1 + 1) );
This compiles although I have found no way to store back the result.
With for_each, there are three possible ways to write it, none of which is supported by Lambda2: std::for_each( nums.begin(),nums.end(), _1 = _1 + 1 ); std::for_each( nums.begin(),nums.end(), _1 += 1 ); std::for_each( nums.begin(),nums.end(), ++_1 ); Of those, the first one is impossible because op= must be a member, and the latter two are possible in principle, but fall outside the initial scope of the library. There's also std::transform( nums.begin(),nums.end(), nums.begin(), _1 + 1 );