allocator.construct 循环是否等于 std::uninitialized_copy?
在此上下文中,T
是某种类型,allocator
是该类型的分配器对象。默认情况下它是 std::allocator
我有一块由 allocator.allocate(n)
获取的内存。我还有一个包含 T
对象的容器 con
(例如,std::vector
)。我想用 T
对象初始化该内存块。
内存块的位置存储在T* data
中。
这两个代码示例总是相同吗?
#include <memory>
// example 1
std::uninitialized_copy(con.begin(), con.end(), data)
// example 2
std::vector<T>::const_iterator in = con.begin();
for (T* out = data; in != con.end(); ++out, ++in) {
allocator.construct(out, *in);
}
而对于这两个人呢?
#include <memory>
T val = T(); // could be any T value
// example 3
std::uninitialized_fill(data, data + n, val)
// example 4
for (T* out = data; out != (data + n); ++out) {
allocator.construct(out, val);
}
In this context T
is a certain type and allocator
is an allocator object for that type. By default it is std::allocator<T>
but this is not necessarily true.
I have a chunk of memory acquired by allocator.allocate(n)
. I also have a container con
of T
objects (say, a std::vector<T>
). I want to initialize that chunk of memory with the T
object(s).
The location of the chunk of memory is stored in T* data
.
Are these two code examples always identical?
#include <memory>
// example 1
std::uninitialized_copy(con.begin(), con.end(), data)
// example 2
std::vector<T>::const_iterator in = con.begin();
for (T* out = data; in != con.end(); ++out, ++in) {
allocator.construct(out, *in);
}
And for these two?
#include <memory>
T val = T(); // could be any T value
// example 3
std::uninitialized_fill(data, data + n, val)
// example 4
for (T* out = data; out != (data + n); ++out) {
allocator.construct(out, val);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据 这个解释 他们应该做同样的事情,如
allocator::construct
据说构造了对象,而std::uninitialized...
也构造了对象。但我不知道,在实现您自己的allocator::construct
时,标准到底说了些什么以及您有什么自由。编辑:好的,C++03 标准在第 20.1.5 §2 表 32 中规定,
construct(p,t)
应该具有与new ((void*)p) T(t)
(对于任何符合标准的分配器,不仅是std::allocator
)。在 20.4.4.1 §1 中,uninitialized_copy
应该与20.4.4.2 §1 中的
uninitialized_fill
具有相同的效果,所以我认为这不会不要给他们留下任何不同行为的空间。所以回答你的问题:是的,确实如此。
According to this explanations They should do the same, as
allocator::construct
is said to construct the object andstd::uninitialized...
also constructs the objects. But I do not know, what exactly the standard says and what freedom you have, when implementing your ownallocator::construct
.EDIT: Ok, the C++03 standard states in section 20.1.5 §2 table 32, that
construct(p,t)
should have the same effect asnew ((void*)p) T(t)
(for any standard compliant allocator, not onlystd::allocator
). And in 20.4.4.1 §1, thatuninitialized_copy
should have the same effect asand in 20.4.4.2 §1, that
uninitialized_fill
has an effect ofSo I think that doesn't leave any room for them to behave differently. So to answer your question: yes, it does.
C++ 入门第 5 章 §13.6.2:
C++ Primer 5th §13.6.2: