Boost logo

Boost :

Subject: Re: [boost] [local] Help for the Alternatives section
From: Yechezkel Mett (ymett.on.boost_at_[hidden])
Date: 2011-03-29 07:51:15


On Sun, Mar 27, 2011 at 3:59 PM, Lorenzo Caminiti <lorcaminiti_at_[hidden]> wrote:
> On Sat, Mar 26, 2011 at 6:19 PM, Lorenzo Caminiti <lorcaminiti_at_[hidden]> wrote:
>> How can I program the following code (its local function and `factor`
>> constant binding) using Boost.Phoenix, Boost.Lambda, and C++0x lambda
>
> ***Thanks for the examples but is there a way to make `factor` const
> in the closure?***
>

C++0x lambdas:

#include <iostream>
#include <vector>
#include <algorithm>

int main() {
  double sum = 0.0;
  int factor = 10;

  std::vector<double> v(3);
  v[0] = 1.0; v[1] = 2.0; v[2] = 3.0;
  std::for_each(v.begin(), v.end(), [&, factor](double num){
      sum += factor * num;
      std::cout << "Summed: " << sum << std::endl;
  });
  return 0;
}

Since factor is captured by copy it can't be modified (unless the
keyword "mutable" appears after the parameter list).

Yechezkel Mett


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk