Converting a BOOST_FOREACH to use the new c++ 11 lambda syntax

Hi Fokes: 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? Any help appreciated. Regards Sean.

On 01/01/13 16:34, Sean Farrow wrote:
Hi Fokes:
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?
Why don't you use C++11 range-for loops instead?

On Tue, Jan 01, 2013 at 05:09:58PM +0100, Mathias Gaunard wrote:
On 01/01/13 16:34, Sean Farrow wrote:
I’m trying to convert a BOOST_FOREACH loop to use the new lambda syntax:
Why don't you use C++11 range-for loops instead?
A wild guess is that lambdas have wider compiler support than range-for, across the board. Never underestimate the power of being able to target a wider set of deployed compilers, even if they're inferior. -- Lars Viklund | zao@acc.umu.se

On 01/01/2013 08:09 AM, Mathias Gaunard wrote:
On 01/01/13 16:34, Sean Farrow wrote:
Hi Fokes:
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?
Why don't you use C++11 range-for loops instead?
My guess is Sean is using an MS compiler which doesn't have range-for )-: -- Michael Caisse ciere consulting ciere.com

On 01/01/2013 07:34 AM, Sean Farrow wrote:
Hi Fokes:
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));
});
Hi Sean - This will work fine. I personally prefer to be more selective in my captures when it is so specific. Instead of the capture all by ref [&] I might use [&IniSections]. michael -- Michael Caisse ciere consulting ciere.com

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
participants (5)
-
Lars Viklund
-
Mathias Gaunard
-
Michael Caisse
-
Nathan Ridge
-
Sean Farrow