的unique_ptr提振相同呢? [英] unique_ptr boost equivalent?
问题描述
有没有在升压库C ++ 1X的性病一些等价类::的unique_ptr?我在寻找的是能够有一个异常安全的工厂函数,行为像这样...
的std ::的unique_ptr<基地以及GT; create_base()
{
返回的std ::的unique_ptr<基地以及GT(新的派生);
}无效some_other_function()
{
的std ::的unique_ptr<基地以及GT; B = create_base(); //做一些东西用b,可能会或可能不会引发异常... //现在b为自动的破坏。
}
编辑:现在,我使用这个技巧,这似乎是最好的,我可以在这一点上得到...
基础* create_base()
{
返回新的派生;
}无效some_other_function()
{
提高:: scoped_ptr的<基地以及GT; B = create_base(); //做一些东西用b,可能会或可能不会引发异常... //现在b为自动地删除。
}
这是不可能没有的C ++ 0x(它是标准的一部分,以创建像的unique_ptr
库,所以升压并不需要提供它)。
没有具体右值引用,这是C ++ 0x中,一个健壮的实现中的unique_ptr
A的功能是不可能的,有或无的推动作用。
在C ++ 03,有几个可能的选择,虽然各自有各自的缺陷。
-
的boost :: shared_ptr的
大概是capabilites方面最简单的更换。您可以放心地使用它的任何地方你,否则会使用一个的unique_ptr
键,它会工作。它只是不会被视为有效,因为添加的引用计数。但是,如果你正在寻找一个简单的简易替换这是能够处理一切的unique_ptr
能做到的,这可能是你最好的选择。 (当然,一个的shared_ptr
可以做更多的事情为好,但它也可以简单地作为一个下拉更换为的unique_ptr
) -
的boost :: scoped_ptr的
类似于的unique_ptr
,但不允许所有权转移。只要智能指针是为了保持其整个生命周期独家拥有它的伟大工程。 -
的std :: auto_ptr的
作品非常相似,的unique_ptr
,但有一些限制,主要是它不能存储在标准库的容器中。如果你只是寻找一个指针,使所有权转移,但并不意味着要存储在容器中或周围复制,这可能是一个不错的选择。
Is there some equivalent class for C++1x's std::unique_ptr in the boost libraries? 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).
Specifically without rvalue references, which are a feature in C++0x, a robust implementation of unique_ptr
is impossible, with or without Boost.
In C++03, there are a few possible alternatives, although each have their flaws.
boost::shared_ptr
is probably the simplest replacement in terms of capabilites. You can safely use it anywhere you'd otherwise use aunique_ptr
and it'd work. 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 everythingunique_ptr
can do, this is probably your best bet. (Of course, ashared_ptr
can do a lot more as well, but it can also simply be used as a drop-in replacement forunique_ptr
.)boost::scoped_ptr
is similar tounique_ptr
but does not allow transfer of ownership. It works great as long as the smart pointer is meant to retain exclusive ownership throughout its lifetime.std::auto_ptr
works very similar tounique_ptr
, but has a few limitations, mainly that it can not be stored in standard library containers. 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.
这篇关于的unique_ptr提振相同呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!