<legend id='vKmtA'><style id='vKmtA'><dir id='vKmtA'><q id='vKmtA'></q></dir></style></legend>

  1. <tfoot id='vKmtA'></tfoot>

    <small id='vKmtA'></small><noframes id='vKmtA'>

    <i id='vKmtA'><tr id='vKmtA'><dt id='vKmtA'><q id='vKmtA'><span id='vKmtA'><b id='vKmtA'><form id='vKmtA'><ins id='vKmtA'></ins><ul id='vKmtA'></ul><sub id='vKmtA'></sub></form><legend id='vKmtA'></legend><bdo id='vKmtA'><pre id='vKmtA'><center id='vKmtA'></center></pre></bdo></b><th id='vKmtA'></th></span></q></dt></tr></i><div id='vKmtA'><tfoot id='vKmtA'></tfoot><dl id='vKmtA'><fieldset id='vKmtA'></fieldset></dl></div>
        <bdo id='vKmtA'></bdo><ul id='vKmtA'></ul>

      unique_ptr 提升等效吗?

      时间:2023-07-20
        <bdo id='ZjOW6'></bdo><ul id='ZjOW6'></ul>
      • <legend id='ZjOW6'><style id='ZjOW6'><dir id='ZjOW6'><q id='ZjOW6'></q></dir></style></legend>
              <tbody id='ZjOW6'></tbody>

              <tfoot id='ZjOW6'></tfoot>

              <i id='ZjOW6'><tr id='ZjOW6'><dt id='ZjOW6'><q id='ZjOW6'><span id='ZjOW6'><b id='ZjOW6'><form id='ZjOW6'><ins id='ZjOW6'></ins><ul id='ZjOW6'></ul><sub id='ZjOW6'></sub></form><legend id='ZjOW6'></legend><bdo id='ZjOW6'><pre id='ZjOW6'><center id='ZjOW6'></center></pre></bdo></b><th id='ZjOW6'></th></span></q></dt></tr></i><div id='ZjOW6'><tfoot id='ZjOW6'></tfoot><dl id='ZjOW6'><fieldset id='ZjOW6'></fieldset></dl></div>
              1. <small id='ZjOW6'></small><noframes id='ZjOW6'>

              2. 本文介绍了unique_ptr 提升等效吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                boost 库中是否有 C++1x 的 std::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.
                }
                

                现在,我正在使用这个 hack,这似乎是我目前所能得到的最好的......

                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.
                }
                

                推荐答案

                如果没有 C++0x,就不可能创建像 unique_ptr 这样的东西(它是标准库的一部分,所以 Boost 不不需要提供).

                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.

                在 C++03 中,有几种可能的替代方案,尽管每种方案都有其缺陷.

                In C++03, there are a few possible alternatives, although each have their flaws.

                • boost::shared_ptr 可能是功能方面最简单的替代品.你可以安全地在任何地方使用它,否则它会使用 unique_ptr 并且它会工作.由于添加了引用计数,它的效率不会那么高.但是,如果您正在寻找能够处理 unique_ptr 可以做的所有事情的简单的替代品,这可能是您最好的选择.(当然,shared_ptr 也可以做更多的事情,但它也可以简单地用作 unique_ptr 的替代品.)
                • boost::scoped_ptrunique_ptr 类似,但不允许转让所有权.只要智能指针旨在在其整个生命周期内保持独占所有权,它就可以很好地工作.
                • std::auto_ptr 的工作原理与 unique_ptr 非常相似,但有一些限制,主要是它不能存储在标准库容器中.如果您只是在寻找一个允许所有权转移的指针,但并不打算将其存储在容器中或四处复制,那么这可能是一个不错的选择.
                • boost::shared_ptr is probably the simplest replacement in terms of capabilites. You can safely use it anywhere you'd otherwise use a unique_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 everything unique_ptr can do, this is probably your best bet. (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.)
                • boost::scoped_ptr is similar to unique_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 to unique_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 提升等效吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何使用 Visual Studio 2015(企业)构建 boost 版本 1.58.0 下一篇:boost::property_tree XML 漂亮打印

                相关文章

              3. <small id='ltHts'></small><noframes id='ltHts'>

                • <bdo id='ltHts'></bdo><ul id='ltHts'></ul>
                <i id='ltHts'><tr id='ltHts'><dt id='ltHts'><q id='ltHts'><span id='ltHts'><b id='ltHts'><form id='ltHts'><ins id='ltHts'></ins><ul id='ltHts'></ul><sub id='ltHts'></sub></form><legend id='ltHts'></legend><bdo id='ltHts'><pre id='ltHts'><center id='ltHts'></center></pre></bdo></b><th id='ltHts'></th></span></q></dt></tr></i><div id='ltHts'><tfoot id='ltHts'></tfoot><dl id='ltHts'><fieldset id='ltHts'></fieldset></dl></div>
              4. <legend id='ltHts'><style id='ltHts'><dir id='ltHts'><q id='ltHts'></q></dir></style></legend>
                <tfoot id='ltHts'></tfoot>