【问题标题】:Alternative library/template class for the deprecated auto_ptr in C++17C++17 中已弃用的 auto_ptr 的替代库/模板类
【发布时间】:2017-07-06 21:52:08
【问题描述】:

拥有必须使用各种编译器 C++17 C++03 等编译的共享项目库。因此,如果代码为保持通用。

查看#ifdef 类型的解决方案,但它变得一团糟。 再加上对久经考验的编码进行编辑并不是一个特别有吸引力的想法!

有谁知道替换auto_ptr 的形式、尺寸和功能?

【问题讨论】:

  • 你可以做的一件事是,如果你有 C++11 或更高版本,你可以使用unique_ptr,或者之前使用shared_ptr(如果没有std::shared_ptr,甚至可以使用boost::shared_ptr)。这样可以避免auto_ptr 的问题
  • 如果它仅在库内部,您可以使用 #ifdef 解析为 c++03 的 auto_ptr 和 c++11 及更高版本的 unique_ptr,另一个为空白或std::move,用于所有权转让。如果它是外部的,你不能确定用户不会滥用auto_ptr
  • 顺便说一句,OP 似乎对使用宏的解决方案不感兴趣。

标签: c++ replace auto-ptr


【解决方案1】:

Boost.MoveBoost Smart Pointerstd::unique_ptrstd::auto_ptr 可以替换为 boost::movelib::unique_ptrstd::shared_ptrstd::weak_ptr boost::shared_ptrboost::weak_ptr

而且,当不打算转让所有权时,boost::scoped_ptr 可以让您更有表现力。

【讨论】:

    猜你喜欢
    • 2017-03-14
    • 2018-06-02
    • 2016-01-17
    • 2020-03-03
    • 1970-01-01
    • 2017-06-09
    • 1970-01-01
    • 2020-06-02
    • 2013-04-28
    相关资源
    最近更新 更多