C++ Logo

std-proposals

Advanced search

Re: [std-proposals] New function for std::vector

From: Magnus Fromreide <magfr_at_[hidden]>
Date: Sat, 28 Dec 2024 01:37:10 +0100
On Sat, Dec 28, 2024 at 12:10:55AM +0000, Jonathan Wakely via Std-Proposals wrote:
> On Fri, 27 Dec 2024, 22:51 Chris Ryan via Std-Proposals, <
> std-proposals_at_[hidden]> wrote:
>
> > Hi Andre,
> > .resize(0) actually deletes the storage.
> >
>
> No it doesn't. clear() and resize(0) both preserve capacity.
>
>
> Farrakh is wanting it to preserve the storage (keep the .capacity(), not
> > have to alloc it again)
> >
>
> That's what clear() does.
>
> The only reason to add a new function would be to avoid the destructor
> calls for the elements, which can be accomplished with a custom Allocator
> that has an empty destroy function.

Wouldn't that lead to the member objects not beeing destructed when the
vector's lifetime ends?

To me it sounds like Farrakh wants an extra abstraction on top of vector which
maintains it's own size which is <= vector.size.

This should be possible to do using a decorator like

template<class C>
class Farrakh {
private:
 C c;
 typename C::size_type size_ = 0;
public:
 // lots of members that forward to c but uses size_ rather than
 // c.size(), but in particular these:

 void push_back(auto v) {
  if (size_ < c.size())
   c[size_++] = v;
  else
   c.push_back(v);
 }
 void clear() { size_ = 0; }
};

I am strongly against anything like this beeing added to the standard.

>
>
> > Farrakh,
> > I think that you might not realize the reason .clear() is slower is
> > because it has to call the in-place destructor for each element (zero
> > through size(), but not all the way up to .capacity()) You can't just
> > change the count or you screw up the object lifetime of those objects
> > that were not destructed. D'tors won't get called. Index positions may
> > potentially get overwritten later with subsequent push_backs(...) and the
> > in-place c'tors calls. (thus causing leaks)
> >
> > https://en.cppreference.com/w/cpp/container/vector/clear
> >
> >
> > On Fri, Dec 27, 2024 at 2:08 PM Andre Kostur via Std-Proposals <
> > std-proposals_at_[hidden]> wrote:
> >
> >> What’s wrong with .resize(0) ?
> >>
> >> On Fri, Dec 27, 2024 at 1:57 PM Фаррах Фаттахов via Std-Proposals <
> >> std-proposals_at_[hidden]> wrote:
> >>
> >>> After working with SFML, I so hated std::vector (I was a bad programmer
> >>> back then and didn't know about the reserve() function, which led to not
> >>> understanding why std::vector is so slow, while a simple array performs
> >>> much better). As a result, in my new project i created my own version of
> >>> std::vector - FE2D::dynamic_array. And there, from the beginning i created
> >>> one useful function - reset()
> >>> ```C++
> >>>
> >>> // Reset the Size of Dynamic Array to Zero, but it's don't Touches the Real Occupied Memory of Dynamic Array
> >>> void reset() { mSize = 0; }
> >>> ```
> >>>
> >>> Instead of clear() it's just setting mSize to zero. It's very useful in
> >>> the situations like
> >>>
> >>> ```C++
> >>> void Render() { // It's called every frame
> >>> // Add a sprites to the FE2D::dynamic_array to draw it later
> >>> m_Renderer.AddSprite(m_Sprite_0);
> >>> m_Renderer.AddSprite(m_Sprite_1);
> >>> m_Renderer.AddSprite(m_Sprite_2);
> >>> ...
> >>> // Draw the Sprites and reset the dynamic arrays to prepare it for the next frame
> >>> m_Renderer.RenderSprites();
> >>> }
> >>> ```
> >>> Here reset() works like clear(), but much faster
> >>>
> >>> In the std::vector or std::deque is no functions like reset() and i want
> >>> to add it there, especially sometimes i get errors because my
> >>> FE2D::dynamic_array is not so good like std::vector and i have to use it
> >>> instead of mine, but there is no function i need. I think reset() will be
> >>> nice thing for std::vector.
> >>> --
> >>> Std-Proposals mailing list
> >>> Std-Proposals_at_[hidden]
> >>> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
> >>>
> >> --
> >> Std-Proposals mailing list
> >> Std-Proposals_at_[hidden]
> >> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
> >>
> > --
> > Std-Proposals mailing list
> > Std-Proposals_at_[hidden]
> > https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
> >

> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2024-12-28 00:37:20