std::allocator 构造/销毁与放置 new/p->~T()

发布于 2024-07-14 17:32:46 字数 718 浏览 4 评论 0原文

对于我的一个项目,我正在从头开始编写一些 STL 容器(我有我的理由)。 由于我如此密切地模仿 STL 的功能和接口,因此我会尽最大努力遵守“如果它与标准结构同名,它将尽可能符合标准”的政策。

因此,当然我的容器将分配器作为模板参数,这非常好,因为它允许一些自定义分配方案。 回答我的问题。

std::allocator 接口将内存分配与对象构造分开。 同样,它将释放与销毁分开。 这是有道理的,因为从哪里获取内存或多或少与在 C++ 中正确构造对象无关。

因此,有两个构造/释放函数,默认实现如下所示(直接从书本中摘取):

void construct(pointer p, const T& val)    { new(p) T(val); }
void destroy(pointer p)                    { p->~T(); }

如您所见,构造只是调用新的放置,而销毁只是调用析构函数。

是否有任何理由使用这些而不是仅使用放置新和析构函数语法? “正确的”分配器可以以另一种方式实现这些吗? 或者我是否保证所有符合标准的分配器实现都会有以这种方式实现的构造/销毁方法?

更重要的是,可以肯定地说我始终可以使用 std::uninitialized_copy 和 std::uninitialized_fill 来构造容器的元素吗?

谢谢。

For a project of mine, I am writing some STL containers from scratch (I have my reasons). Since I am mimicking the functionality and interfaces of the STL so closely I am doing my best to keep with the policy "if it has the same name as a standard construct, it will conform to the standard as much as possible."

So, of course my containers take allocators as a template parameter, which is very nice as it allows for some custom allocation schemes. On to my question.

The std::allocator interface separates memory allocation from object construction. Likewise it separates deallocation from destruction. This makes sense since where you get memory from is more or less irrelevant to properly constructing an object in c++.

So there are two construction/deallocation functions which look like this for the default implementation (lifted straight from a book):

void construct(pointer p, const T& val)    { new(p) T(val); }
void destroy(pointer p)                    { p->~T(); }

as you can see construct simply calls placement new and destroy simply calls the destructor.

Is there any reason to use these over just using placement new and destructor syntax? can a "correct" allocator implement these in another way? Or am I guaranteed that all allocator implementations which conform to the standard will have there construct/destroy methods implemented in this way?

More to the point, is it safe to say that I can always use the std::uninitialized_copy and std::uninitialized_fill for constructing the elements of my containers?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

趴在窗边数星星i 2024-07-21 17:32:47

这只是为了隐藏方法中分配的细节。 即,我们提供了构造和销毁的API,将来我们可以改变实现。 现在我们使用placement new来分配内存,以后如果我们想改变分配方式,只需要改变这两个方法即可。

This is just to hide the details of allocation in a method. i.e., we are providing the APIs for construction and destruction, in the future we can change the implementation. Now we are using placement new to allocate the memory, in the future if we want to change the allocation we just have to change these two methods.

救赎№ 2024-07-21 17:32:46

分配器可以在构造/销毁之前和之后添加日志记录语句,或者它关心的任何其他副作用。

当然,实际的构造必须通过调用placement new和析构函数来进行,但规则手册中并没有说构造/销毁函数中不能发生任何其他事情

The allocator could add logging statements before and after construction/destruction, or any other side effects it cared to do.

Of course the actual construction has to occur by calling placement new and the destructor, but it doesn't say in the rulebook that nothing else must happen in the construct/destroy functions

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文