简体   繁体   English

我应该从使用 boost::shared_ptr 切换到 std::shared_ptr 吗?

[英]Should I switch from using boost::shared_ptr to std::shared_ptr?

I would like to enable support for C++0x in GCC with -std=c++0x .我想使用-std=c++0x 0x 在 GCC 中启用对 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.我不一定需要 GCC 4.5(以及很快 4.6)中当前支持的 C++11 功能,但我想开始习惯它们。 For example, in a few places where I use iterators, an auto type would be useful.例如,在我使用迭代器的一些地方, auto类型会很有用。

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".这里的目标是鼓励我将新标准的特性纳入我的编程“词汇表”。

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?根据您对 C++11 支持的了解,在 GCC 中启用它是一个好主意,然后通过例如从使用boost::shared_ptr切换到std::shared_ptr来完全接受它,因为两者不混合?

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. PS:我知道这个很好的问题,它比较了shared_ptr的不同风格,但我要求在标准最终确定之前使用更高级别的建议。 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?另一种说法是,当像 GCC 这样的编译器说它支持“实验性功能”时,这是否意味着我在编译过程中可能会遇到奇怪的错误,这将是主要的时间消耗和 StackOverflow 上神秘问题的来源?

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切换回来,因为我只是不相信它在 GCC 4.5 中的支持,如this question中的示例所示

There are a couple of reasons to switch over to std::shared_ptr :切换到std::shared_ptr有几个原因:

  • You remove a dependency on Boost.您删除了对 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.根据您的编译器和调试器,调试器可能对std::shared_ptr是“智能的”,并直接显示指向 object 的位置,而不是说 boost 的实现。 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.至少在 Visual Studio 中, std::shared_ptr在调试器中看起来像一个普通的指针,而boost::shared_ptr则暴露了一堆 boost 的内部结构。
  • 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. (理论上——我自己没有测试过)至少从 2014-07-22 开始, boost::shared_ptr理解移动语义。
  • 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 .) std::shared_ptr在数组类型上正确使用delete [] ,而boost::shared_ptr在这种情况下会导致未定义的行为(您必须使用shared_array或自定义删除器) (实际上我是正确的。看到这个- 专门针对仅unique_ptr ,而不是shared_ptr 。)

And one major glaring reason not to:一个主要的明显理由不:

  • You'd be limiting yourself to C++11 compiler and standard library implementations.您将自己限制在 C++11 编译器和标准库实现中。

Finally, you don't really have to choose.最后,您实际上不必选择。 (And if you're targeting a specific compiler series (eg 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) (如果您的目标是特定的编译器系列(例如 MSVC 和 GCC),您可以轻松地将其扩展为使用std::tr1::shared_ptr (如果可用)。不幸的是,似乎没有检测 TR1 支持的标准方法)

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

I suppose it depends how much you use boost.我想这取决于你使用多少升压。 I personally only use it very sparingly (in fact the random number library, in a single project).我个人只非常谨慎地使用它(实际上是随机数库,在一个项目中)。 I've recently started using -std=c++0x for my other projects, and I use the new std:: library features like shared_ptr in them.我最近开始在我的其他项目中使用-std=c++0x ,并且我在其中使用了新的 std:: 库功能,例如 shared_ptr。 I like my projects to have the minimum of dependencies, so I'd rather be dependent on the compiler's standard library implementation than on boost.我喜欢我的项目具有最少的依赖关系,所以我宁愿依赖编译器的标准库实现而不是 boost。

But I don't think there is a one-size-fits-all answer to this question.但我不认为这个问题有一个万能的答案。

You should always use std::shared_ptr wherever possible, if it's available, instead of boost.如果可用,您应该始终尽可能使用std::shared_ptr而不是 boost。 This is basically because all new interfaces which use shared_ptr will use the Standard shared ptr.这基本上是因为所有使用shared_ptr的新接口都将使用标准共享 ptr。

It's probably not a bad idea to start getting into the habit of using std::shared_ptr when allowed by the compiler.在编译器允许的情况下开始养成使用 std::shared_ptr 的习惯可能不是一个坏主意。 Since the interface is the same as boost's shared_ptr you can always switch back if you needed to.由于接口与 boost 的 shared_ptr 相同,因此如果需要,您可以随时切换回来。

Aside from implementation consistency, boost::shared_ptr currently retains at least two niche advantages over std::shared_ptr :除了实现的一致性之外, boost::shared_ptr目前与std::shared_ptr相比至少保留了两个利基优势:

If you are just building on the one platform that is fine (make the switch)如果您只是在一个很好的平台上构建(进行切换)
(Note: You do have unit tests to validate backward compatibility don't you?) (注意:您确实有单元测试来验证向后兼容性,不是吗?)

If you compile on multiple platforms is where it becomes a little more awkward as you need to validate that the features on g++ 4.5 are available on all the platforms you use (ie building for MAC/Linux the default Mac g++ compiler is still a couple of version's behind the default compilers on Linux).如果您在多个平台上编译会变得有点尴尬,因为您需要验证 g++ 4.5 上的功能在您使用的所有平台上都可用(即为 MAC/Linux 构建默认的 Mac g++ 编译器仍然是几个版本落后于 Linux 上的默认编译器)。

Another reason to switch over to std::shared_ptr : it supports std::unique_ptr , ie has constructor.切换到std::shared_ptr的另一个原因:它支持std::unique_ptr ,即具有构造函数。

boost::shared_ptr doesn't. boost::shared_ptr没有。

I found std::shared_ptr to be faster than boost::shared_ptr.我发现 std::shared_ptr 比 boost::shared_ptr 快。 I did a test, you can review the code and see the pie chart results comparing boost, Qt, and std shared pointers.我做了一个测试,你可以查看代码并查看对比boost、Qt和std共享指针的饼图结果。

在此处输入图像描述

https://www.osletek.com... https://www.osletek.com...

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

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