我想知道当我有这样的函数时会发生什么:
typedef std::unordered_map<std::string,std::string> stringmap;
stringmap merge (stringmap a,stringmap b)
{
stringmap temp(a); temp.insert(b.begin(),b.end()); return temp;
}
Run Code Online (Sandbox Code Playgroud)
函数返回时会发生什么?
'temp'是否在被销毁之前复制到临时r值(超出范围)并且C++ 11可能是NRVO-ptimized(因此'temp'的副本直接写入返回目标槽)?
什么都不应该复制.有两种可能性:
temp被移动到调用者范围内的对象; 要么temp成为调用者范围内对象的别名.这被称为"返回值优化".在2011年之前(具体而言,没有移动语义),第一种情况需要复制而不是移动.第二种情况允许在C++ 98和11中使用.
| 归档时间: |
|
| 查看次数: |
308 次 |
| 最近记录: |