如何只对move构造函数进行一次调用?
How do I make only a single call to the move-constructor?
我如何使下面的代码只调用移动构造函数一次?
MC
MC
#include <vector>
#include <map>
#include <memory>
#include <iostream>
struct Bar
{
Bar() { }
Bar( Bar&& rhs )
{
std::cerr << "MCn";
for( auto& p : rhs.m_v )
{
std::cerr << "inside loopn";
m_v.push_back( move( p ));
}
}
std::vector< std::unique_ptr< Bar >> m_v;
};
int main()
{
Bar b;
std::map<int,Bar> m;
m.insert( std::make_pair( 1, std::move( b )));
}
编辑
看起来emplace
是正确的答案-但不幸的是,它还没有在gcc 4.7.2 ... ...有没有办法我可以把它混叠到insert
然后在它正确实现的时候把它去掉?
使用std::map::emplace
:
m.emplace(1, std::move(b));
实质上是用emplace
代替insert
:
m.emplace(1, std::move(b));
相关文章:
- 使用仅使用一次的变量调用的复制构造函数.这可能是通过调用move构造函数进行编译器优化的情况吗
- 循环中的条件:为什么每次都调用strlen(),而vector.size()只调用一次
- 为什么 zlib 放气初始化调用一次不起作用?
- 什么是仅调用一次并调用参数的控制台应用
- 谷歌基准测试,如何只调用一次代码?
- 循环中本地对象的析构函数是否保证在下一次迭代之前被调用?
- 调用函数一次用于动态链接库,一次从可执行文件调用函数
- 在可变函数调用中执行一次语句
- 我应该只在免费商店上为一次调用创建对象吗?
- 如果初始化需要shared_from_this,如何在一次调用中构建一个类
- GoogleMock:如何保存参数,以便在模拟上的下一次调用中使用
- C++错误,隐 <function-style-cast> 式要求使用模板化类一次调用多个构造函数的多个转换
- C++ 向量元素被最后一次调用push_back替换
- 在一次调用C++11中启动多个线程
- opengl/glut 中的 renderDisplayFunc 不止一次调用 myfunc
- 为什么popn()一次又一次调用返回相同的输出
- 如何只对move构造函数进行一次调用?
- 通过一次调用c++将整个二进制文件读取到一个数组中
- 在一次调用中重置使用 iomanip 所做的所有更改的类:setprecision、setfill 和标志
- 圆函数:一次调用绘制多个圆