【问题标题】:can I build boost libraries with std::shared_ptr instead of boost::shared_ptr我可以用 std::shared_ptr 而不是 boost::shared_ptr 构建 boost 库吗
【发布时间】:2016-03-15 13:58:55
【问题描述】:

在 c++11 项目中,我想尽可能多地使用 stl 原语而不是 boost 原语。虽然我仍然需要一些 boost 库——例如文件系统。它虽然使用 boost::shared_ptr。

我的问题是 - 有没有办法让 boost 切换到使用 std::shared_ptr 以及如何?

【问题讨论】:

  • 为什么要改变 boost 的内部结构?在你的代码中使用std::shared_ptr,让 boost 使用它需要的任何东西。
  • 是内部的吗?没有任何库的接口期望或返回 shared_ptr?
  • @gsf:函数仅在转移对象所有权时才采用/返回shared_ptr。很少有 Boost 库实际上需要声明用户传入的东西的所有权。所以大多数参数要么是值类型、引用,要么是裸(非拥有)指针。确实,我想不出在界面中使用shared_ptr 的Boost 函数。
  • @NicolBolas 这很有道理,我也不能,但我之前没有广泛使用过 boost 文件系统。

标签: c++ c++11 boost stl


【解决方案1】:

Boost 是否有配置设置以使其使用std::shared_ptr?一般不会。 Boost.Asio apparently has a way to make the internals use std::shared_ptr(似乎在std::shared_ptr 可用时自动打开)。但是没有 Boost 范围的设置。

您可能可以做一些查找替换体操来使其发挥作用。当然,这是假设这些 Boost 库都没有对 Boost 的内部结构做出假设。而且他们都无法访问其内部。而且boost::shared_ptr和std::shared_ptr没有接口区别。

这不值得付出努力。让 Boost 在内部使用它想要的任何代码。对您来说唯一重要的地方是接口,很少 Boost 库实际上在代码的接口中使用boost::shared_ptr。事实上,我在 Boost 1.59 文档中对 std::shared_ptr 进行了 Google 网站搜索,我发现它被引用的唯一地方是我上面链接的文档。

【讨论】:

  • 我强烈建议您不要使用 std shared_ptr 查找和替换 boost shared_ptr。 Boost 的 shared_ptr 是 C++ 11 shared_ptr 的强大超集,实际上 C++ 1y shared_ptr 预计会变得更像 Boost 的。 Boost 使用 C++ 1y 扩展,所以它不会是直接交换。
  • @NiallDouglas:什么是 C++1y?你说的是 C++14 还是 C++17 还是什么?
  • 是的,你是对的,我的意思是 C++1z 道歉。那是“C++ 17”,当然任何改进的 shared_ptr 都不会作为 1z 的一部分,而是作为一些提议的库 TS 大约在同一时间。
猜你喜欢
  • 2015-04-04
  • 1970-01-01
  • 2011-09-27
  • 2013-06-12
  • 2013-04-16
  • 2011-09-13
  • 2012-09-01
  • 1970-01-01
相关资源
最近更新 更多