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

发布于 2024-11-15 10:44:11 字数 909 浏览 3 评论 0 原文

我想使用 -std=c++0x 在 GCC 中启用对 C++0x 的支持。我绝对不一定需要 GCC 中的任何当前支持的 C++11 功能 4.5(很快就会有 4.6),但我想开始习惯它们。例如,在我使用迭代器的一些地方,auto 类型会很有用。

但同样,我不需要需要任何当前支持的功能。这里的目标是鼓励我将新标准的功能纳入我的编程“词汇”中。

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

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

编辑:我决定从 std::shared_ptr 切换回来,因为我只是不相信它在 GCC 4.5 中的支持 此问题中的示例所示

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

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: 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?

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

无戏配角 2024-11-22 10:44:11

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

  • 您删除了对 Boost 的依赖。
  • 调试器。根据您的编译器和调试器,调试器可能会“智能”地处理 std::shared_ptr 并直接显示指向的对象,而这不会是 boost 的实现。至少在 Visual Studio 中,std::shared_ptr 看起来像调试器中的普通指针,而 boost::shared_ptr 则公开了一堆 boost 的内部结构。
  • 您链接的问题中定义的其他新功能。
  • 您将获得一个几乎可以保证启用移动语义的实现,这可能会节省一些引用计数修改。 (理论上——我自己没有测试过) 至少截至 2014 年 7 月 22 日,boost::shared_ptr 理解移动语义。
  • std::shared_ptr 在数组类型上正确使用delete [],而 boost::shared_ptr 在这种情况下会导致未定义的行为(您必须使用 shared_array 或自定义删除器)(实际上我的立场是正确的。请参阅this - 此专业化仅适用于 unique_ptr,而不是shared_ptr。)

还有一个不这样做的明显原因:

  • 您将自己限制在 C++11 编译器和标准库实现上。

最后,你实际上不必选择。 (如果您的目标是特定的编译器系列(例如 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

There are a couple of reasons to switch over to std::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.)

And one major glaring reason not to:

  • You'd be limiting yourself to C++11 compiler and standard library implementations.

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
茶色山野 2024-11-22 10:44:11

我想这取决于你使用boost的程度。我个人很少使用它(实际上是随机数库,在一个项目中)。我最近开始在我的其他项目中使用 -std=c++0x ,并且在其中使用新的 std:: 库功能,例如共享指针。我希望我的项目具有最少的依赖性,因此我宁愿依赖编译器的标准库实现,而不是 boost。

但我认为这个问题没有一个一刀切的答案。

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

But I don't think there is a one-size-fits-all answer to this question.

半岛未凉 2024-11-22 10:44:11

您应该尽可能使用 std::shared_ptr (如果可用),而不是 boost。这基本上是因为所有使用 shared_ptr 的新接口都将使用标准共享指针。

You should always use std::shared_ptr wherever possible, if it's available, instead of boost. This is basically because all new interfaces which use shared_ptr will use the Standard shared ptr.

围归者 2024-11-22 10:44:11

在编译器允许的情况下开始养成使用 std::shared_ptr 的习惯可能不是一个坏主意。由于接口与 boost 的shared_ptr 相同,因此如果需要,您可以随时切换回来。

It's probably not a bad idea to start getting into the habit of using std::shared_ptr when allowed by the compiler. Since the interface is the same as boost's shared_ptr you can always switch back if you needed to.

南风起 2024-11-22 10:44:11

如果您只是在一个平台上进行构建,那就可以了(进行切换)
(注意:您确实有单元测试来验证向后兼容性,不是吗?)

如果您在多个平台上进行编译,情况会变得有点尴尬,因为您需要验证 g++ 4.5 上的功能是否在您的所有平台上可用。使用(即为 MAC/Linux 构建默认的 Mac g++ 编译器仍然比 Linux 上的默认编译器落后几个版本)。

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

三生路 2024-11-22 10:44:11

切换到 std::shared_ptr 的另一个原因:
它支持std::unique_ptr,即有构造函数。

boost::shared_ptr 没有。

Another reason to switch over to std::shared_ptr:
it supports std::unique_ptr, i.e. has constructor.

boost::shared_ptr doesn't.

可是我不能没有你 2024-11-22 10:44:11

std::shared_ptr 至少保留了两个利基优势:

  • 除了实现一致性之外,boost::shared_ptr 目前比 boost.org/doc/libs/1_53_0/libs/smart_ptr/make_shared_array.html" rel="nofollow">boost::make_shared_noinit。它与数组结合使用特别有用,可以避免零初始化的成本和单独分配的开销。 (FWIW,这也是建议添加 符合标准。)
  • Boost.Python 特别利用了 boost::shared_ptr 对类型擦除自定义删除器的支持,但是 尚未对 std::shared_ptr 执行相同操作。

Aside from implementation consistency, boost::shared_ptr currently retains at least two niche advantages over std::shared_ptr:

烟花肆意 2024-11-22 10:44:11

我发现 std::shared_ptr 比 boost::shared_ptr 更快。我做了一个测试,您可以查看代码并查看比较 boost、Qt 和 std 共享指针的饼图结果。

在此处输入图像描述

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

I found std::shared_ptr to be faster than 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.

enter image description here

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文