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
表示下面的表达式是良构的:
T
isCopyInsertable
intoX
means that the following expression is well-formed:
allocator_traits<A>::construct(m, p, v);
MoveInsertable
由标准定义:
T
is MoveInsertable
into X
表示下面的表达式是良构的:
T
isMoveInsertable
intoX
means 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::forward
ed.
Again, v
and rv
are std::forward
ed.
当然,它们会分别调用复制或移动构造函数.
Of course, these will invoke the copy or move constructors respectively.
所以:
T
isCopyInsertable
intoX
:X
的分配器可以放置-new 构造T
的元素,传递T
类型的表达式T
isMoveInsertable
intoX
:X
的分配器可以放置-new 构造T
的元素,传递T
类型的右值
T
isCopyInsertable
intoX
: the allocator forX
can placement-new construct an element ofT
, passing an expression of typeT
T
isMoveInsertable
intoX
: the allocator forX
can placement-new construct an element ofT
, passing an rvalue of typeT
这篇关于MoveInsertable 和 CopyInsertable 之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:MoveInsertable 和 CopyInsertable 之间的区别?
- STL 中有 dereference_iterator 吗? 2022-01-01
- 静态初始化顺序失败 2022-01-01
- 近似搜索的工作原理 2021-01-01
- 使用/clr 时出现 LNK2022 错误 2022-01-01
- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01
- Stroustrup 的 Simple_window.h 2022-01-01
- 如何对自定义类的向量使用std::find()? 2022-11-07
- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01
- 从python回调到c++的选项 2022-11-16
- C++ 协变模板 2021-01-01