Tom*_*P89 0 c++ move-semantics c++11
我一直在玩c ++ 11移动语义
在代码中......
#include <vector>
#include <string>
std::vector<std::string> GetNewVector()
{
std::vector<std::string> newVec;
newVec.push_back(std::string("hello")); //(1)
newVec.push_back(std::string("whey")); //(2)
return newVec;
}
int main(int argc, char* argv[])
{
std::vector<std::string> vec = GetNewVector();
}
Run Code Online (Sandbox Code Playgroud)
在点(1),当对象移动到向量中时,调用"hello"对象的移动构造函数.
在第(2)点,首先再次调用"hello"的移动构造函数,(我假设这是向量重新分配的位置),然后调用"whey"移动构造函数.
这完全符合预期,但是我期望在结束时返回向量时再次移动对象GetNewVector(),但是移动构造函数不会再次被调用.我的猜测是RVO正在发生,但是当我在调试模式下运行Visual Studio(2k10)时,我不确定是否会发生这种情况?
如果可以执行RVO,那么它将优先于使用移动构造函数吗?
| 归档时间: |
|
| 查看次数: |
259 次 |
| 最近记录: |