Difference between MoveInsertable and CopyInsertable?(MoveInsertable 和 CopyInsertable 之间的区别?)
问题描述
有人可以对这两个术语提供更清晰的解释吗?
Can someone provide a more lucid explanation of these two terms?
换句话说,请用例子做一些简单的解释.
In other words, some simple explanation with an example, please.
(来自:cppreference.com)
(from : cppreference.com)
MoveInsertable : 指定一个类型的右值可以复制到未初始化的存储中.
MoveInsertable : Specifies that a rvalue of the type can be copied in uninitialized storage.
CopyInsertable :指定一个实例type 可以在未初始化的存储中就地复制构造.
CopyInsertable : Specifies that an instance of the type can be copy-constructed in-place, in uninitialized storage.
推荐答案
这些需求是类型 T 和容器 X 之间的关系.容器有一个分配器类型,A,用于为其包含的对象分配内存.
These requirements are a relationship between a type T and a container X. A container has an allocator type, A, which it uses to allocate the memory for its contained objects.
如果 m 是这些分配器之一,p 是 T*,rv 是 rv 类型的右值code>T 和 v 类型为 T 的表达式:
If m is one of these allocators, p a T*, rv an rvalue of type T, and v an expression of type T:
CopyInsertable由标准定义:
T is CopyInsertable into X 表示下面的表达式是良构的:
TisCopyInsertableintoXmeans that the following expression is well-formed:
allocator_traits<A>::construct(m, p, v);
MoveInsertable 由标准定义:
T is MoveInsertable into X 表示下面的表达式是良构的:
TisMoveInsertableintoXmeans that the following expression is well-formed:
allocator_traits<A>::construct(m, p, rv);
现在要理解这些定义,我们必须知道 allocator_traits<A>::construct 做了什么.很简单,在这种情况下它调用:
Now to understand these definitions, we must know what allocator_traits<A>::construct does. Quite simply, in this case it calls:
m.construct(p, v) // CopyInsertable case
m.construct(p, rv) // MoveInsertable case
v 和 rv 在这里仍然有各自的值类别,因为 std::forward 应用于 allocator_traits<A>::construct.
v and rv still have their respective value categories here because std::forward is applied to the argument of allocator_traits<A>::construct.
那么分配器的construct 成员函数有什么作用呢?好吧,正如你所料,它在 p 位置构造了一个 T 类型的对象,方法是:
So what does an allocators construct member function do? Well, as you might expect, it constructs an object of type T at the location p by doing:
::new ((void*)p) T(v) // CopyInsertable case
::new ((void*)p) T(rv) // MoveInsertable case
同样,v 和 rv 是 std::forwarded.
Again, v and rv are std::forwarded.
当然,它们会分别调用复制或移动构造函数.
Of course, these will invoke the copy or move constructors respectively.
所以:
TisCopyInsertableintoX:X的分配器可以放置-new 构造T的元素,传递T 类型的表达式TisMoveInsertableintoX:X的分配器可以放置-new 构造T的元素,传递T 类型的右值
TisCopyInsertableintoX: the allocator forXcan placement-new construct an element ofT, passing an expression of typeTTisMoveInsertableintoX: the allocator forXcan placement-new construct an element ofT, passing an rvalue of typeT
这篇关于MoveInsertable 和 CopyInsertable 之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:MoveInsertable 和 CopyInsertable 之间的区别?
基础教程推荐
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 常量变量在标题中不起作用 2021-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- 这个宏可以转换成函数吗? 2022-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
