
I’m trying to convert a BOOST_FOREACH loop to use the new lambda syntax:
I have a ptree variable declared in a class of which the foreach is part. I also have a vectorstd::wstring that I need to use within my lambda.
This all being the case, the following is the original BOOST_FOREACH:
BOOST_FOREACH(boost::property_tree::ptree::value_type &v, IniTree)
IniSections.push_back(ConvertToWString(v.first));
And the following is the converted lambda:
std::for_each(IniTree.begin(), IniTree.end(), [&] (boost::property_tree::ptree::value_type &v)
{
IniSections.push_back(ConvertToWString(v.first));
});
Is this the right way to go about the conversion—have I missed anything?
That looks correct to me. Note that there is also a boost::for_each function in Boost.Range that allows you to avoid repeating the range expression: boost::for_each(IniTree, [&](boost::property_tree::ptree::value_type &v) { IniSections.push_back(ConvertToWString(v.first)); }); Doesn't make much of a difference in this case, but it can be useful when the range expression is longer. Regards, Nate