是否有像auto_ptr和shared_ptr这样不需要C++0x的通用智能指针?
我想要一个非引用计数智能指针,它可以结合 auto_ptr
和 shared_ptr
的一些有用方面。我认为 C++0x 的 unique_ptr
最终是我所需要的,但我需要可以在 Visual Studio 2008 和 Xcode (gcc 4.2) 上编译的东西。
我需要的功能是:
- 可在工厂方法中使用,以便在复制时转移所有权(如 auto_ptr)
- 支持
release()
(如 auto_ptr) - 可以与前向声明一起使用(如 shared_ptr)
所以,我想它确实是一个更好的 auto_ptr
。有没有什么东西可以在 boost 或其他地方做到这一点(注意:我没有时间关注 Loki)?或者我应该自己推出?
编辑:我刚刚阅读了有关 auto_ptr
的更多内容 - 听起来如果您确保每个 .cpp 中包含类标头,您可以将其与前向声明一起使用使用智能指针引用标头的文件(例如,请参阅GotW)。有人对此有任何一般建议或经验法则吗?
编辑2:shared_ptr不可接受的原因是因为我需要一个release()方法,因为我正在通过引入工厂方法来整理一些遗留代码,但它必须与一些手动指针所有权代码共存。在整个代码库中使用shared_ptr 会很棒,但也是一项艰巨的任务。
EDIT3:最后,一旦我发现了前向包含的怪癖,auto_ptr
就足以胜任这项工作。尝试为 shared_ptr
编写一个自定义删除器以允许选择性删除指针也会很有趣。
I'm wanting a non-reference counted smart pointer that can combine some of the useful aspects of auto_ptr
and shared_ptr
. I think that C++0x's unique_ptr
is ultimately what I'd need, but I need something that will compile on Visual Studio 2008 and Xcode (gcc 4.2).
The functionality I need is:
- Usable in factory methods so that ownership is transferred on copying (like auto_ptr)
- Supports
release()
(like auto_ptr) - Can be used with forward declaration (like shared_ptr)
So, I suppose it's really a better auto_ptr
. Is there anything that does this in boost or elsewhere (note: I haven't got the time to wrap my head around Loki)? Or should I just roll my own?
EDIT: I've just been reading more about auto_ptr
-- it sounds like you can use it with forward declarations if you ensure that the class header is included in each .cpp file that references the header with the smart pointer (eg see GotW). Anyone got any general advice or rules of thumb on this?
EDIT2: The reason shared_ptr isn't acceptable is because I need a release() method as I'm tidying up some legacy code by introducing factory methods, but it has to co-exist with some manual pointer ownership code. Using shared_ptr throughout the codebase would be great, but a huge task.
EDIT3: In the end, auto_ptr
was adequate for the job once I discovered the quirks of the forward inclusion. It would be interesting also to try writing a custom deleter for shared_ptr
to allow for optional deletion of the pointer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
编辑:在我原来的答案中,我无法理解您不接受
boost::shared_ptr
(可能是出于性能原因)。正如您所注意到的,
auto_ptr
确实支持前向声明。您需要在可能破坏auto_ptr
引用的对象的位置包含引用类的标头。但请注意,auto_ptr
的语义有点奇怪,需要小心。以下问题的答案包含更多信息,包括为什么
unique_ptr
只能针对 C++0x 实现,因为它需要右值引用:unique_ptr 增强等效项?
Edit: In my original answer, I failed to understand that
boost::shared_ptr
is not acceptable to you (probably for performance reasons).auto_ptr
does, as you note, support forward declaration. You need to include the header for the referenced class in those places that may destroy the object referenced by theauto_ptr
. Note, however, that the semantics ofauto_ptr
are slightly quirky and require some care.The answers to the following question contain more information, including the reason why
unique_ptr
can only be implemented for C++0x because it requires rvalue references:unique_ptr boost equivalent?
C++ TR1(受现代 GCC 和 Visual Studio 支持)内置
shared_ptr
以及其他(参见维基百科)。这些大部分取自Boost,它几乎适用于任何现代编译器。如果您需要严格的所有权,您可以检查
scoped_ptr
。C++ TR1 (supported by modern GCC and Visual Studio) has built-in
shared_ptr
and others (cf. Wikipedia). Those have mostly been taken from Boost which works for pretty much any modern compiler.If you need strict ownership, you may check the
scoped_ptr
.通过一些样板代码,您可以在 C 中近似
unique_ptr
++03。这不是一个完美的模拟。例如,您将需要几个更显式的move
调用,以便能够返回本地unique_ptr
。With some boilerplate code you can approximate
unique_ptr
in C++03. It's not a perfect emulation. You will need a couple more explicitmove
calls, for example, to be able to return a localunique_ptr
.