【问题标题】:unique_ptr/auto_ptr look alike with custom deleter for c++98unique_ptr/auto_ptr 与 c++98 的自定义删除器相似
【发布时间】:2018-09-15 09:51:01
【问题描述】:

auto_ptr 不支持自定义删除器,并且 tr1 shared_ptr 对我来说不是一个好选择。
在 c11 之前是否有任何好的选择,unique_ptr/auto_ptr 看起来与自定义删除器相似?

【问题讨论】:

  • 使用 boost 库实现?
  • 我的 boost 版本是 1.33.1。我 boost/scoped_ptr 没有删除器,而且我似乎无法使用 进行编译。 boost unique_ptr 来自版本 1.57
  • c11 是指C++11
  • @PeretzLevinov 有什么阻碍您更新到 Boost 1.57(或更高版本)吗?
  • 你为什么使用预 C++11 编译器。是由于您使用的操作系统平台的限制还是项目限制?顺便说一句,您永远不应该使用现在已弃用的 auto_ptr。

标签: c++ smart-pointers unique-ptr raii auto-ptr


【解决方案1】:

Boost.Move: unique_ptr (C++03, Boost 1.57)

没有 C++11(及更高版本)的移动语义的 unique_ptr 实现有点棘手,但如果你可以模拟移动语义,你可以继续使用与 std::unique_ptr 类似的语义实现.

Boost 为 C++03 提供了这个实现,作为 Boost.Move 库的一部分:

什么是 Boost.Move?

右值引用是 C++0x 的一个主要特性,支持移动语义 对于 C++ 值。但是,我们不需要 C++0x 编译器来获取 移动语义学的优势。 Boost.Move 模拟 C++0x 移动语义 在 C++03 编译器中,并允许编写有效的可移植代码 最适合 C++03 和 C++0x 编译器。

具体来说,from Boost 1.57 and onwards,提供boost/move/unique_ptr.hpp

//!\file
//! Describes the smart pointer unique_ptr, a drop-in replacement for std::unique_ptr,
//! usable also from C++03 compilers.
//!
//! Main differences from std::unique_ptr to avoid heavy dependencies,
//! specially in C++03 compilers:
//!   - <tt>operator < </tt> uses pointer <tt>operator < </tt>instead of <tt>std::less<common_type></tt>. 
//!      This avoids dependencies on <tt>std::common_type</tt> and <tt>std::less</tt>
//!      (<tt><type_traits>/<functional></tt> headers. In C++03 this avoid pulling Boost.Typeof and other
//!      cascading dependencies. As in all Boost platforms <tt>operator <</tt> on raw pointers and
//!      other smart pointers provides strict weak ordering in practice this should not be a problem for users.
//!   - assignable from literal 0 for compilers without nullptr
//!   - <tt>unique_ptr<T[]></tt> is constructible and assignable from <tt>unique_ptr<U[]></tt> if
//!      cv-less T and cv-less U are the same type and T is more CV qualified than U.

允许向unique_ptr 提供关联的非默认删除器:

//! A unique pointer is an object that owns another object and
//! manages that other object through a pointer.
//! 
//! ...
//!
//! \tparam T Provides the type of the stored pointer.
//! \tparam D The deleter type:
//!   -  The default type for the template parameter D is default_delete. A client-supplied template argument
//!      D shall be a function object type, lvalue-reference to function, or lvalue-reference to function object type
//!      for which, given a value d of type D and a value ptr of type unique_ptr<T, D>::pointer, the expression
//!      d(ptr) is valid and has the effect of disposing of the pointer as appropriate for that deleter.
//!   -  If the deleter's type D is not a reference type, D shall satisfy the requirements of Destructible.
//!   -  If the type <tt>remove_reference<D>::type::pointer</tt> exists, it shall satisfy the requirements of NullablePointer.
template <class T, class D = default_delete<T> >
class unique_ptr
{
    /* ... */
}

【讨论】:

    猜你喜欢
    • 2013-03-30
    • 1970-01-01
    • 1970-01-01
    • 2016-11-23
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多