std::set<T>::value_type T const , not T ; therefore, your lambda argument must be a value type (i.e. a copy) or int const& (and technically, or int const volatile& ), not int& . Ie this works:
std::set<int> collection{2, 3, 4, 5435345, 2}; std::for_each( collection.begin(), collection.end(), [](int const& i) { std::cout << i << std::endl; } );
Bonus question: Also, I would like to change the int& in the lambda argument to auto& , why can't this be automatically output?
Because the standard says it cannot; Historically, I believe this was due to the overly complex interactions between lambdas and concepts (before concepts were removed from the draft). However, I hear rumors that the first defect reporting to the new (C ++ 11) standard will be addressed that way, so you can see support for this choice added to your compiler over the next year or two. EDIT : Oh look, C ++ 14 now has polymorphic lambda ...
ildjarn
source share