为什么在没有std::move的情况下不调用move构造函数
Why move constructor does not called without std::move?
我有这样的代码:
SomeType::SomeType(std::vector<Item>&& container, const float someOtherPArameter)
: internal_container(std::move(container))
{
// some code here
}
有人能向我解释一下为什么move构造函数不调用没有"std::move"的"internal_container"吗?
每当从相同类型的xvalue初始化对象时,都会调用move构造函数。您可以通过调用std::move(x)
来创建该xvalue
。将参数声明为右值引用不会自动使其成为xvalue
。
因为
SomeType::SomeType(std::vector<Item>&& container, const float someOtherPArameter)
: internal_container(container)
{
// the parameter container is in scope here
}
如果在构造函数体内部,对参数container
的访问发现了一个moved-from对象,那将是非常令人惊讶的。(它也会破坏在C++03中完全有效的代码)
这就是为什么您必须明确启用move。
相关文章:
- 在没有太多条件句的情况下,我如何避免被零除
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 在未初始化映射的情况下,将值插入到映射的映射中
- 是默认情况下分配给char数组常量的值
- 为什么我不能在不创建字符串变量的情况下使用函数的字符串输出
- 如何在不产生任何垃圾的情况下获得C中的像素
- 在已经使用Git的情况下减少编译时间
- 为什么在Windows上的VS 2019和Clang 9中"size_t"在没有标题的情况下工作
- 如何在没有信号的情况下从C++执行QML插槽
- 如何在不知道向量大小的情况下输入向量内部的向量?
- 为什么在某些情况下不写入此文件?
- 为什么Mat类的两个对象可以在不重载运算符+的情况下添加
- 如何在没有 std::move 的情况下移动临时对象
- 在无法完成 RVO 的情况下,我们应该写"std::move"吗?
- 在哪些情况下,从 std::forward 分配比从 std::move 分配更可取?为什么
- 如果在没有move构造函数的情况下移动对象,会发生什么
- std::move( ) 在没有 move-ctor 的情况下调用 copy-ctor.为什么以及如何预防它
- 在多个return语句的情况下,返回时带有“std::move”明智
- 为什么在没有std::move的情况下不调用move构造函数
- 如何在没有 std::move 的情况下按值返回unique_ptr