简体   繁体   English

unique_ptr提升等价?

[英]unique_ptr boost equivalent?

Is there some equivalent class for C++1x's std::unique_ptr in the boost libraries? 在boost库中是否有一些与C ++ 1x的std :: unique_ptr等效的类? The behavior I'm looking for is being able to have an exception-safe factory function, like so... 我正在寻找的行为是能够拥有异常安全的工厂功能,就像这样......

std::unique_ptr<Base> create_base()
{
    return std::unique_ptr<Base>(new Derived);
}

void some_other_function()
{
    std::unique_ptr<Base> b = create_base();

    // Do some stuff with b that may or may not throw an exception...

    // Now b is destructed automagically.
}

EDIT: Right now, I'm using this hack, which seems like the best I can get at this point... 编辑:现在,我正在使用这个黑客,这似乎是我能在这一点上得到的最好的......

Base* create_base()
{
    return new Derived;
}

void some_other_function()
{
    boost::scoped_ptr<Base> b = create_base();

    // Do some stuff with b that may or may not throw an exception...

    // Now b is deleted automagically.
}

It's not possible to create something like unique_ptr without C++0x (where it's part of the standard library, and so Boost doesn't need to provide it). 在没有C ++ 0x的情况下创建类似unique_ptr东西是不可能的(它是标准库的一部分,因此Boost不需要提供它)。

Specifically without rvalue references, which are a feature in C++0x, a robust implementation of unique_ptr is impossible, with or without Boost. 特别是没有rvalue引用(这是C ++ 0x中的一个特性),无论是否有Boost,都不可能实现unique_ptr的强大实现。

In C++03, there are a few possible alternatives, although each have their flaws. 在C ++ 03中,有一些可能的替代方案,尽管每个都有它们的缺陷。

  • boost::shared_ptr is probably the simplest replacement in terms of capabilites. boost::shared_ptr可能是功能方面最简单的替代品。 You can safely use it anywhere you'd otherwise use a unique_ptr and it'd work. 你可以安全地在任何你使用unique_ptr地方使用它,它可以工作。 It just wouldn't be as efficient, because of the added reference counting. 由于添加了引用计数,它不会那么高效。 But if you're looking for a simple drop-in replacement that's able to handle everything unique_ptr can do, this is probably your best bet. 但是如果你正在寻找一个能够处理unique_ptr可以做的所有事情的简单替代品,这可能是你最好的选择。 (Of course, a shared_ptr can do a lot more as well, but it can also simply be used as a drop-in replacement for unique_ptr .) (当然, shared_ptr也可以做得更多,但它也可以简单地用作unique_ptr替代品。)
  • boost::scoped_ptr is similar to unique_ptr but does not allow transfer of ownership. boost::scoped_ptr类似于unique_ptr但不允许转让所有权。 It works great as long as the smart pointer is meant to retain exclusive ownership throughout its lifetime. 只要智能指针在其整个生命周期中保留独占所有权,它就能很好地工作。
  • std::auto_ptr works very similar to unique_ptr , but has a few limitations, mainly that it can not be stored in standard library containers. std::auto_ptrunique_ptr非常相似,但有一些限制,主要是它不能存储在标准库容器中。 If you're simply looking for a pointer that allows transfer of ownership, but which is not meant to be stored in containers or copied around, this is probably a good bet. 如果您只是寻找一个允许转让所有权的指针,但这并不意味着存储在容器中或复制,这可能是一个不错的选择。

Starting from Boost 1.57 there's an official unique_ptr implementation in Boost.Move library. Boost 1.57开始, Boost.Move库中有一个官方的unique_ptr实现。

From the documentation : 文档

(...) a drop-in replacement for std::unique_ptr, usable also from C++03 compilers. (...)std :: unique_ptr的替代品,也可以从C ++ 03编译器中使用。

The code is available in <boost/move/unique_ptr.hpp> header file and lives in boost::movelib namespace. 该代码在<boost/move/unique_ptr.hpp>头文件中可用,并且位于boost::movelib命名空间中。 Moreover, Boost.Move library provides make_unique() factory function in <boost/move/make_unique.hpp> , also in boost::movelib namespace. 此外,Boost.Move库在<boost/move/make_unique.hpp>提供了make_unique()工厂函数,也在boost::movelib命名空间中。

Hence the example from the question could be implemented this way: 因此,问题的例子可以这样实现:

#include <boost/move/unique_ptr.hpp>

using boost::movelib::unique_ptr;

unique_ptr<Base> create_base()
{
    return unique_ptr<Base>(new Derived);
}

See a live example on Wandbox . 查看Wandbox上的实例 Note that the code compiles fine with gcc 4.6.4 in C++98 mode (!). 请注意,代码在C ++ 98模式(!)中使用gcc 4.6.4编译得很好。

What's interesting in boost::movelib::unique_ptr when applied to your case with base/derived classes, the implementation provides a compile-time check for the declaration of a virtual destructor in the base class. boost::movelib::unique_ptr在应用于具有基类/派生类的情况时有什么boost::movelib::unique_ptr ,该实现提供了对基类中虚拟析构函数声明的编译时检查。 If you happen to omit it the code won't compile (click the "Run (...)" button to see the compiler error message). 如果您碰巧省略它,代码将无法编译 (单击“运行(...)”按钮以查看编译器错误消息)。

One minor issue is that includes come from boost/move directory but the code lives in boost::movelib namespace (subtle difference but can be annoying). 一个小问题是包括来自boost/move目录,但代码存在于boost::movelib命名空间中(细微差别,但可能很烦人)。

See also a thread on boost mailing list for more details. 有关更多详细信息,另请参阅boost邮件列表中的线程

Thanks to Ion Gaztañaga for this absolutely unique and useful piece of code. 感谢IonGaztañaga这个绝对独特且有用的代码。

You might want to try Howard Hinnant's 'proof of concept' unique_ptr<> implementation for C++03 (disclaimer - I haven't): 你可能想尝试一下Howard Hinnant的'概念证明' unique_ptr<> C ++ 03的实现(免责声明 - 我没有):

One of his examples is returning a unique_ptr<int> : 他的一个例子是返回unique_ptr<int>

unique_ptr<int> factory(int i)
{
    return unique_ptr<int>(new int(i));
}

进程间库中的unique_ptr怎么样?

I've used Howard Hinnant's unique_ptr . 我用过Howard Hinnant的unique_ptr If you are not really good at reading crazy metaprogramming errors from you compiler you might want to steer clear. 如果你不擅长从编译器中读取疯狂的元编程错误,你可能想要明确指出。 It however does act just like a unique_ptr in 90% of the cases. 然而,在90%的情况下它确实像unique_ptr一样。

Otherwise I'd suggest passing paramters as boost::scoped_ptr& and swap internally to steal ownership. 否则我建议将paramters作为boost::scoped_ptr&和内部交换来窃取所有权。 To get unique_ptr style return values use an auto_ptr . 要获取unique_ptr样式返回值,请使用auto_ptr Capture the auto_ptr return value in a shared_ptr or scoped_ptr to avoid using the auto_ptr directly. 捕获shared_ptrscoped_ptrauto_ptr返回值以避免直接使用auto_ptr

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM