When using BOOST_FOREACH
, is the following code safe?
BOOST_FOREACH (const std::string& str, getStrings())
{
...
}
...
std::vector<std::string> getStrings()
{
std::vector<std::string> strings;
strings.push_back("Foo");
...
return strings;
}
Or should I grab a copy of the container before calling BOOST_FOREACH
, e.g.:
const std::vector<std::string> strings = getString();
BOOST_FOREACH (const std::string& str, strings)
{
...
}
In the first example is there any danger that BOOST_FOREACH
could end up calling getStrings()
multiple times?
And although BOOST_FOREACH is a macro, it is a remarkably well-behaved one. It evaluates its arguments exactly once, leading to no nasty surprises
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With