Boost C++ 库

...世界上最受推崇和专业设计的 C++ 库项目之一。 Herb SutterAndrei Alexandrescu, C++ 编码标准

PrevUpHomeNext

类模板 polymorphic_allocator

boost::container::pmr::polymorphic_allocator

概要

// In header: <boost/container/pmr/polymorphic_allocator.hpp>

template<typename T> 
class polymorphic_allocator {
public:
  // types
  typedef T value_type;

  // public member functions
  polymorphic_allocator() noexcept;
  polymorphic_allocator(memory_resource *) noexcept;
  polymorphic_allocator(const polymorphic_allocator &) noexcept;
  template<typename U> 
    polymorphic_allocator(const polymorphic_allocator< U > &) noexcept;
  polymorphic_allocator & operator=(const polymorphic_allocator &) noexcept;
  T * allocate(size_t);
  void deallocate(T *, size_t) noexcept;
  template<typename U, class ... Args> void construct(U *, Args &&...);
  template<typename U> void destroy(U *);
  polymorphic_allocator select_on_container_copy_construction() const noexcept;
  memory_resource * resource() const noexcept;
};

描述

类模板 polymorphic_allocator 的特化符合 Allocator 要求。使用不同的内存资源构造,相同特化的 polymorphic_allocator 的不同实例可以表现出完全不同的分配行为。这种运行时多态性允许使用 polymorphic_allocator 的对象表现得好像它们在运行时使用了不同的分配器类型,即使它们使用相同的静态分配器类型。

polymorphic_allocator 公有成员函数

  1. polymorphic_allocator() noexcept;

    效果: 将 m_resource 设置为 get_default_resource()

  2. polymorphic_allocator(memory_resource * r) noexcept;

    要求: r 非空。

    效果: 将 m_resource 设置为 r。

    抛出: 无

    注意: 此构造函数提供从 memory_resource* 的隐式转换。

  3. polymorphic_allocator(const polymorphic_allocator & other) noexcept;

    效果: 将 m_resource 设置为 other.resource()。

  4. template<typename U> 
      polymorphic_allocator(const polymorphic_allocator< U > & other) noexcept;

    效果: 将 m_resource 设置为 other.resource()。

  5. polymorphic_allocator & 
    operator=(const polymorphic_allocator & other) noexcept;

    效果: 将 m_resource 设置为 other.resource()。

  6. T * allocate(size_t n);

    返回: 等效于 static_cast<T*>(m_resource->allocate(n * sizeof(T), alignof(T)))

  7. void deallocate(T * p, size_t n) noexcept;

    要求: p 是从内存资源 x 分配的,x 等于 *m_resource,使用 x.allocate(n * sizeof(T), alignof(T))

    效果: 等效于 m_resource->deallocate(p, n * sizeof(T), alignof(T))。

    抛出: 无。

  8. template<typename U, class ... Args> void construct(U * p, Args &&... args);

    要求: 使用分配器 *this 和构造函数参数 std::forward<Args>(args)... 的 T 的 Uses-allocator 构造是良构的。[注意:对于不使用分配器的类型,uses-allocator 构造始终是良构的。——尾注]

    效果: 通过 uses-allocator 构造,使用分配器 *this 和构造函数参数 std::forward<Args>(args)... 在 p 处构造一个 T 对象。

    抛出: 无,除非 T 的构造函数抛出异常。

  9. template<typename U> void destroy(U * p);

    效果: p->~U()。

  10. polymorphic_allocator select_on_container_copy_construction() const noexcept;

    返回: 等效于 polymorphic_allocator()

  11. memory_resource * resource() const noexcept;

    返回: m_resource。


PrevUpHomeNext