unique_ptr / auto_ptr похожи с пользовательским средством удаления для C ++ 98 - PullRequest
0 голосов
/ 15 сентября 2018

auto_ptr не поддерживает пользовательское удаление, а tr1 shared_ptr - не лучший вариант для меня.
Есть ли хорошие варианты до c11 для unique_ptr / auto_ptr, которые выглядят одинаково с пользовательским удалением?

1 Ответ

0 голосов
/ 15 сентября 2018

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

Реализация unique_ptr без семантики перемещения в C ++ 11 (и более поздних версиях) немного сложна, но если бы вы могли эмулировать семантику перемещения, вы могли бы перейти к реализации, аналогичной семантике std::unique_ptr.

Boost предоставил эту самую реализацию для C ++ 03 как часть библиотеки Boost.Move:

Что такое Boost.Move?

Rvalue-ссылки являются основной функцией C ++ 0x, позволяющей семантику перемещения для значений C ++. Однако нам не нужны компиляторы C ++ 0x, чтобы Преимущество ходовой семантики. Boost.Move эмулирует семантику перемещения C ++ 0x в компиляторах C ++ 03 и позволяет писать переносимый код, который работает оптимально в компиляторах C ++ 03 и C ++ 0x.

В частности, с Boost 1.57 и далее , обеспечивая 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
{
    /* ... */
}
...