Why does a.push_back(b) not work in lambda?

 list<double> temp; list<double> result = std::accumulate( vec.begin(), vec.end(), temp, [](const std::list<double>& a, double b) { return b<2.0 ? a.push_back(b): a ; }); 

    1 answer 1

    A link to a constant object is passed to your lamba expression, and you are trying to change its internal state via push_back . To solve your problem, simply remove the const modifier:

     [](std::list<double>& a, double b) 

    You also have another error related to the return value in lamba. She expects to receive an instance of the type list<double> (i.e. decltype(temp) ), and in your case one of the return values ​​is the type void (the result of a.push_back(b) ).

    Thus, the working version:

     list<double> result = std::accumulate(vec.begin(), vec.end(), temp, [](std::list<double>& a, double b) { if (b < 2.0) a.push_back(b); return a; } );