Let's say I want to create a vector container that , unlike std :: vector , allows you to uninitialize the storage. Using a container, for example vec <T>, would be something like this:
The user explicitly indicates that the vector should allocate N uninitialized elements as follows:
vec <T> a(N, no_init);
At some point, when the data is known, the user explicitly initializes the element in position nusing the arguments args...:
a.init(n, args...);
OR, which is equivalent, creates the item manually:
new (&a[n]) T(args...);
Other operations can be initialized or copied more massively (for example, std::uninitialized_copy), but this is only for convenience; The basic basic operation is the same.
After performing some task, the vector may be left with some initialized elements, while others may not. The vector does not contain any additional information, therefore, before freeing memory, it will either destroy all elements or destroy only depending on T.
I am sure that this can be done, only I am not sure about the consequences. Naturally, we would like this structure to be safe for all types T, assuming that the user is not trying to use an uninitialized element before creating it. This may sound like a strong assumption, but access to elements only within the vector range is not so different from the assumption, and it is so common.
So my questions are:
, vec <T> a(no_init)? , is_pod , , is_trivial. , .
? , ? is_trivially_destructible? , , ( ), .
, ?
, , , , std::get_temporary_buffer (, operator new()), . std::vector::emplace_back(), .