C++ Logo

std-proposals

Advanced search

Re: C++ create a class with array members which are not constructed.

From: Nikolay Mihaylov <nmmm_at_[hidden]>
Date: Wed, 2 Sep 2020 21:56:34 +0300
This definitely breaks lots of things.

but vector works in similar way:

using T = std::string;
std::vector<T> v;
v.reserve(100); // there are 100 unconstructed T (strings) there.

std::optional can also do similar

using T = std::string;
std::optional<T> v[100]; // there are 100 unconstructed T (strings) there.



On Wed, Sep 2, 2020 at 9:46 PM Steve Hearnden via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> I was looking at a performance issue, with some code and found that the
> cost of creating the dynamic heap memory was the significant cost.
>
>
>
> It would have been handy to have a class which implemented something like
> std::vector, but with a **small** buffer of values to use as an
> alternative piece of memory when the vector size was below a threshold.
>
> Unfortunately, an array of the constructed type, would cause the
> constructor to be called for each array element, and for complex types,
> could undo the benefit of the pre-bought memory.
>
>
>
> My proposal would be for classes (or structures) to have the ability for
> a member to be declared with an attribute
>
>
>
> [[unconstructed]]
>
> which would cause none of the constructors to actually construct the
> object.
>
> The class would not naturally also call destructors.
>
>
>
> e.g.
>
> template< class T, size_t size>
>
> class temp_vector
>
> {
>
> [[unconstructed]] T _V[size];
>
> size_t m_constructed = 0;
>
> public:
>
> ~temp_vector()
>
> {
>
> ///// Destroy the array manually.
>
> for( size_t i = 0; i < m_constructed; i++
> ){
>
> _V[i].~T();
>
> }
>
> }
>
> }
>
>
>
>
>
> Sent from Mail <https://go.microsoft.com/fwlink/?LinkId=550986> for
> Windows 10
>
>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>

Received on 2020-09-02 14:00:40