可能吗? std :: vector< double> my_vec(sz);已分配但未初始化或填充的 [英] Is it possible? std::vector<double> my_vec(sz); which is allocated but not initialized or filled
问题描述
在[ C ++ 11和std :: vector构造函数,Channel72问,
At [Value-Initialized Objects in C++11 and std::vector constructor, Channel72 asks,
问题:我的理解在这里正确吗?如果T是POD,显式std :: vector(size_type count)是否提供未初始化的数组(类似于malloc)?
答案是否定的。
我的问题是,好吧,那是什么?
My question is, "Okay then, what does?"
其中之一是内文(Nevin)暗示要回答我的问题。为了澄清,我的问题是,是否可以使用std :: vector< double>而不用零或其他东西无偿地填充分配的内存?
One of the responses, by Nevin, hints at answering my question. To clarify, my question is, Is there a way to use std::vector<double> without it gratuitously filling allocated memory with zeros or whatever?
我不要求解决方法,例如以零大小启动向量并使用push_back()。这并非总是可能的,此外,在这一点上,我想弄清楚它的原因除了我想弄清楚之外。
I am not asking for workarounds, like starting the vector at zero size and using push_back(). That is not always possible, and besides, at this point I want to get it figured out for no other reason than I want to get it figured out.
Nevin的建议,一个自定义分配器,进行编译。 VC ++ 2017rc(Dinkum)以其通常难以理解的方式抱怨。关于std :: _ Wrap_alloc的信息。 Nevin的代码不完整,我可能不知道如何完成它。在看到他之前,我写了我自己的自定义分配器,该分配器似乎可以正常工作,但是我对自己的理解不够自信,无法发誓。
I cannot get Nevin's suggestion, a custom allocator, to compile. VC++ 2017rc (Dinkum) complains in its usual inscrutable way. Something about std::_Wrap_alloc. Nevin's code is incomplete, and I probably do not know how to complete it. Before I saw his, I wrote my own custom allocator which seems to work, but I am not confident in my understanding enough to swear by it.
花了很多钱,我本来可以写出条条框框的std :: vector替代品,再加上《伟大的美国小说》的几章。
For the time I have spent puzzling over this, I could have written a less dogmatic replacement for std::vector, plus several chapters of the Great American Novel.
推荐答案
万岁!理查德·克里特(Richard Critten)营救!他对这个问题的评论直接引出了答案。
HOORAY! Richard Critten to the rescue! His comment under the question leads directly to the answer.
零位元凶是默认的分配器模板,即std :: allocator。因此,我们替换它,或使用分配器适配器对其进行修改。
The zero-spewing culprit is the default allocator template, namely std::allocator. So we replace it, or modify it with an allocator adapter.
我整理了一下代码,并扩展了注释。 Bill,请随时发布更全面的答案。但是下面的代码很好地完成了这个技巧。
I tidied up the code a little, and expanded the comments. Bill, please feel free to post a more comprehensive answer. But the following does the trick very nicely.
// Allocator adapter
// Given an allocator A, (std::allocator by default), this adapter
// will, when feasible, override A::construct() with a version that
// employs default construction rather than value-initialization.
// "Feasible" means the object (U *ptr) is default-constructable and
// the default constructor cannot throw exceptions.
//
// Thus it thwarts gratuitous initializations to zeros or whatever.
template <typename T, typename A = std::allocator<T>>
class default_init_allocator : public A {
typedef std::allocator_traits<A> a_t;
public:
// http://en.cppreference.com/w/cpp/language/using_declaration
using A::A; // Inherit constructors from A
template <typename U> struct rebind {
using other =
default_init_allocator
< U, typename a_t::template rebind_alloc<U> >;
};
template <typename U>
void construct(U* ptr)
noexcept(std::is_nothrow_default_constructible<U>::value) {
::new(static_cast<void*>(ptr)) U;
}
template <typename U, typename...Args>
void construct(U* ptr, Args&&... args) {
a_t::construct(static_cast<A&>(*this),
ptr, std::forward<Args>(args)...);
}
};
这篇关于可能吗? std :: vector< double> my_vec(sz);已分配但未初始化或填充的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!