使用时BOOST_FOREACH,以下代码是否安全?
BOOST_FOREACH (const std::string& str, getStrings())
{
...
}
...
std::vector<std::string> getStrings()
{
std::vector<std::string> strings;
strings.push_back("Foo");
...
return strings;
}
Run Code Online (Sandbox Code Playgroud)
或者我应该在打电话之前抓住容器的副本BOOST_FOREACH,例如:
const std::vector<std::string> strings = getString();
BOOST_FOREACH (const std::string& str, strings)
{
...
}
Run Code Online (Sandbox Code Playgroud)
在第一个例子中是否存在BOOST_FOREACH可能最终getStrings()多次呼叫的危险?
| 归档时间: |
|
| 查看次数: |
1965 次 |
| 最近记录: |