我应该使用boost :: shared_ptr的到std :: shared_ptr的切换? [英] Should I switch from using boost::shared_ptr to std::shared_ptr?

查看:422
本文介绍了我应该使用boost :: shared_ptr的到std :: shared_ptr的切换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现与 -std =的C ++ 0x 在GCC的C ++ 0x的支持。我并不完全一定需要任何目前在GCC 4.5(很快支持4.6 C ++ 11功能的的),但我想开始习惯他们。例如,在我使用的迭代器的几个地方,一个汽车类型将是有益的。

I would like to enable support for C++0x in GCC with -std=c++0x. I don't absolutely necessarily need any of the currently supported C++11 features in GCC 4.5 (and soon 4.6), but I would like to start getting used to them. For example, in a few places where I use iterators, an auto type would be useful.

但同样,我也不需要任何当前支持​​的功能。这里的目标是鼓励我,以纳入新标准的功能融入我的编程词汇表。

But again, I don't need any of the currently supported features. The goal here is to encourage me to incorporate the features of the new standard into my programming "vocabulary".

从你知道C ++ 11的支持是什么,它​​是一个好主意,使其能够在GCC,然后拥抱它,例如,使用的boost :: shared_ptr的的std :: shared_ptr的完全成为两个不混?

From what you know of the C++11 support, is it a good idea to enable it in GCC, and then embrace it by, for example, switching from using boost::shared_ptr to std::shared_ptr exclusively as the two don't mix?

PS:我所知道的<一个href=\"http://stackoverflow.com/questions/1086798/differences-between-different-flavours-of-shared-ptr\">this好问题其中比较的shared_ptr 但我要问哪个标准最终确定之前,使用更高级别的建议的不同口味。撇开这的另一种方式是,当一个像GCC编译器说,它支持一个实验性功能,这是否意味着我很可能编译,将成为主要的时间汇和对计算器古怪的问题源时遇到奇怪的错误?

PS: I'm aware of this good question which compares the different flavors of shared_ptr but I'm asking a higher level advice on which to use before the standard is finalized. Another way of putting that is, when a compiler like GCC says it supports an "experimental feature", does that mean I am likely to encounter weird errors during compilation that will be major time sinks and a source of cryptic questions on StackOverflow?

修改:我决定改用从的std :: shared_ptr的回来,因为我只是不GCC 4.5作为信托的支持<一个HREF =htt​​p://stackoverflow.com/q/6322364/627517>以身作则,这个问题所示。

Edit: I decided to switch back from std::shared_ptr because I just don't trust its support in GCC 4.5 as shown by example in this question.

推荐答案

有几个原因切换到的 的std :: shared_ptr的

There are a couple of reasons to switch over to std::shared_ptr:


  • 您消除对加速的依赖。

  • 调试器。根据你的编译器和调试器,调试器可能是聪明关于的std :: shared_ptr的,并显示有针对性的直接对象,它不会为说,升压的实现。至少在Visual Studio中,的std :: shared_ptr的看起来像在调试一个普通的指针,而的boost :: shared_ptr的公开了一堆提升的内脏的。

  • 在链接的问题定义的
  • 等新功能。

  • 你得到这几乎是保证被移动语义启用,这可能会节省一些修改引用计数的实现。 (理论上 - 我不是这个测试我自己)截至2014年7月22日,至少的boost :: shared_ptr的理解语义的移动
  • 的std :: shared_ptr的正确使用删除[] 的数组类型,而的boost :: shared_ptr的导致在这种情况下未定义行为(必须使用 shared_array 或定制删除)(其实我的立场。修正请参见 - 专业化因为这是为的unique_ptr 而已,不是的shared_ptr

  • You remove a dependency on Boost.
  • Debuggers. Depending on your compiler and debugger, the debugger may be "smart" about std::shared_ptr and show the pointed to object directly, where it wouldn't for say, boost's implementation. At least in Visual Studio, std::shared_ptr looks like a plain pointer in the debugger, while boost::shared_ptr exposes a bunch of boost's innards.
  • Other new features defined in your linked question.
  • You get an implementation which is almost guaranteed to be move-semantics enabled, which might save a few refcount modifications. (Theoretically -- I've not tested this myself) As of 2014-07-22 at least, boost::shared_ptr understands move semantics.
  • std::shared_ptr correctly uses delete [] on array types, while boost::shared_ptr causes undefined behavior in such cases (you must use shared_array or a custom deleter) (Actually I stand corrected. See this -- the specialization for this is for unique_ptr only, not shared_ptr.)

和一大明显的理由不:


  • 您会限制自己的C ++编译器11和标准库的实现。

最后,你真的没有选择。 (如果你正在针对特定的编译器系列(如MSVC和GCC),你可以很容易地扩展这个使用的std :: TR1的:: shared_ptr的时可用。不幸的似乎没有被检测到TR1支持)的标准方式。

Finally, you don't really have to choose. (And if you're targeting a specific compiler series (e.g. MSVC and GCC), you could easily extend this to use std::tr1::shared_ptr when available. Unfortunately there doesn't seem to be a standard way to detect TR1 support)

#if __cplusplus > 199711L
#include <memory>
namespace MyProject
{
    using std::shared_ptr;
}
#else
#include <boost/shared_ptr.hpp>
namespace MyProject
{
    using boost::shared_ptr;
}
#endif

这篇关于我应该使用boost :: shared_ptr的到std :: shared_ptr的切换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆