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?