c++11 动态内存管理-提供关于分配器类型的信息(std::allocator_traits)(四)

提供关于分配器类型的信息

std::allocator_traits

template< class Alloc >
struct allocator_traits;

(C++11 起)

allocator_traits 类模板提供访问分配器 (Allocator) 各种属性的标准化方式。标准容器和其他标准库组件通过此模板访问分配器,这使得能以任何类类型为分配器,只要用户提供的 allocator_traits 特化实现所有要求的功能。

返回分配器所支持的最大对象大小

std::allocator_traits<Alloc>::max_size

static size_type max_size( const Alloc& a ) noexcept;

(C++11 起)

若可能,则通过调用 a.max_size() ,从 a 获得最大理论可行的分配大小。

若上述行为不可行(例如 a 无成员函数 max_size() ),则返回 std::numeric_limits<size_type>::max() (C++17 前)std::numeric_limits<size_type>::max() / sizeof(value_type) (C++17 起)

参数

(无)

返回值

理论最大分配大小

获得复制标准容器后使用的分配器

std::allocator_traits<Alloc>::select_on_container_copy_construction

static Alloc select_on_container_copy_construction( const Alloc& a );

(C++11 起)

若可能,则获得分配器 a 的赋值构造函数版本,通过调用 a.select_on_container_copy_construction() 。若上述不可行(例如 a 无成员函数 select_on_container_copy_construction() ),则返回不修改的 a 。

此函数为所有标准库容器的复制构造函数所调用。它允许构造函数参数所用的分配器为正在复制的容器所具备,并在需要的情况下修改状态。

参数

a - 标准容器用以传递给容器复制构造函数的分配器

返回值

复制构造的标准容器所用的分配器。

调用示例

#include <iostream>
#include <memory>

struct Cell
{
    int x;
    int y;

    Cell()
    {
        std::cout << __FUNCTION__;
    }
    Cell(int a, int b): x(a), y(b)
    {
        std::cout << __FUNCTION__;
    }
    ~Cell()
    {
        std::cout << __FUNCTION__;
    }


    bool operator ==(const Cell &cell) const
    {
        return x == cell.x && y == cell.y;
    }

    bool operator <(const Cell &cell) const
    {
        if (x < cell.x)
        {
            return true;
        }
        return y < cell.y;
    }
};

std::ostream &operator<<(std::ostream &os, const Cell &cell)
{
    os << "{" << cell.x << "," << cell.y << "}";
    return os;
}

int main()
{
    //若可能,则通过调用 a.max_size() ,从 a 获得最大理论可行的分配大小。
    //若上述行为不可行(例如 a 无成员函数 max_size() ),
    //则返回 std::numeric_limits<size_type>::max() (C++17 前)
    //std::numeric_limits<size_type>::max() / sizeof(value_type) (C++17 起)
    std::allocator_traits<std::allocator<char>> allocator_traitsChar;
    std::cout << "std::allocator_traits<std::allocator<char>> max_size:  "
              << allocator_traitsChar.max_size(std::allocator<int>()) << std::endl;

    std::allocator_traits<std::allocator<int>> allocator_traitsInt;
    std::cout << "std::allocator_traits<std::allocator<int>> max_size:  "
              << allocator_traitsInt.max_size(std::allocator<int>()) << std::endl;

    std::allocator_traits<std::allocator<uint32_t>> allocator_traitsUInt;
    std::cout << "std::allocator_traits<std::allocator<uint32_t>> max_size:  "
              << allocator_traitsUInt.max_size(std::allocator<uint32_t>()) << std::endl;

    std::allocator_traits<std::allocator<long>> allocator_traitsLong;
    std::cout << "std::allocator_traits<std::allocator<long>> max_size:  "
              << allocator_traitsLong.max_size(std::allocator<long>()) << std::endl;

    std::allocator_traits<std::allocator<double>> allocator_traitsDouble;
    std::cout << "std::allocator_traits<std::allocator<double>> max_size:  "
              << allocator_traitsDouble.max_size(std::allocator<double>()) << std::endl;

    std::allocator_traits<std::allocator<Cell>> allocator_traitsCell;
    std::cout << "std::allocator_traits<std::allocator<Cell>> max_size:  "
              << allocator_traitsCell.max_size(std::allocator<Cell>()) << std::endl;

    return 0;
}

输出

std::allocator_traits<std::allocator<char>> max_size:  4294967295
std::allocator_traits<std::allocator<int>> max_size:  1073741823
std::allocator_traits<std::allocator<uint32_t>> max_size:  1073741823
std::allocator_traits<std::allocator<long>> max_size:  1073741823
std::allocator_traits<std::allocator<double>> max_size:  536870911
std::allocator_traits<std::allocator<Cell>> max_size:  536870911

猜你喜欢

转载自blog.csdn.net/qq_40788199/article/details/135188114
今日推荐