命名空间
变体
操作

std::uses_allocator_construction_args

来自 cppreference.cn
< cpp‎ | 内存
 
 
内存管理库
(仅作说明*)
未初始化内存算法
(C++17)
(C++17)
(C++17)
受约束的未初始化
内存算法
C 库

分配器
uses_allocator_construction_args
(C++20)
内存资源
垃圾回收支持
(C++11)(直到 C++23)
(C++11)(直到 C++23)
(C++11)(直到 C++23)
(C++11)(直到 C++23)
(C++11)(直到 C++23)
(C++11)(直到 C++23)
未初始化存储
(直到 C++20*)
(直到 C++20*)
显式生命周期管理
 
定义于头文件 <memory>
T 不是 std::pair 的特化
template< class T, class Alloc, class... Args >

constexpr auto uses_allocator_construction_args( const Alloc& alloc,

    Args&&... args ) noexcept;
(1) (C++20 起)
Tstd::pair 的特化
template< class T, class Alloc, class Tuple1, class Tuple2 >

constexpr auto uses_allocator_construction_args( const Alloc& alloc,

    std::piecewise_construct_t, Tuple1&& x, Tuple2&& y ) noexcept;
(2) (C++20 起)
template< class T, class Alloc >
constexpr auto uses_allocator_construction_args( const Alloc& alloc ) noexcept;
(3) (C++20 起)
template< class T, class Alloc, class U, class V >

constexpr auto uses_allocator_construction_args( const Alloc& alloc,

    U&& u, V&& v ) noexcept;
(4) (C++20 起)
template< class T, class Alloc, class U, class V >

constexpr auto uses_allocator_construction_args( const Alloc& alloc,

    std::pair<U, V>& pr ) noexcept;
(5) (C++23 起)
template< class T, class Alloc, class U, class V >

constexpr auto uses_allocator_construction_args( const Alloc& alloc,

    const std::pair<U, V>& pr ) noexcept;
(6) (C++20 起)
template< class T, class Alloc, class U, class V >

constexpr auto uses_allocator_construction_args( const Alloc& alloc,

    std::pair<U, V>&& pr ) noexcept;
(7) (C++20 起)
template< class T, class Alloc, class U, class V >

constexpr auto uses_allocator_construction_args( const Alloc& alloc,

    const std::pair<U, V>&& pr ) noexcept;
(8) (C++23 起)
template< class T, class Alloc, class NonPair >

constexpr auto uses_allocator_construction_args( const Alloc& alloc,

    NonPair&& non_pair ) noexcept;
(9) (C++20 起)

准备通过 uses-allocator 构造 创建给定类型 T 对象所需的参数列表。

1) 仅当 T 不是 std::pair 的特化时,此重载才参与重载决议。返回如下确定的 std::tuple
2) 仅当 Tstd::pair 的特化时,此重载才参与重载决议。对于 std::pair<T1, T2>T,等价于
return std::make_tuple(std::piecewise_construct,
    std::apply([&alloc](auto&&... args1)
        {
            return std::uses_allocator_construction_args<T1>(alloc,
                       std::forward<decltype(args1)>(args1)...);
        }, std::forward<Tuple1>(x)
    ),
    std::apply([&alloc](auto&&... args2)
        {
            return std::uses_allocator_construction_args<T2>(alloc,
                       std::forward<decltype(args2)>(args2)...);
        }, std::forward<Tuple2>(y)
    )
);
3) 仅当 Tstd::pair 的特化时,此重载才参与重载决议。等价于
return std::uses_allocator_construction_args<T>(alloc,
    std::piecewise_construct, std::tuple<>{}, std::tuple<>{}
);
4) 仅当 Tstd::pair 的特化时,此重载才参与重载决议。等价于
return std::uses_allocator_construction_args<T>(alloc,
    std::piecewise_construct,
    std::forward_as_tuple(std::forward<U>(u)),
    std::forward_as_tuple(std::forward<V>(v))
);
5,6) 仅当 Tstd::pair 的特化时,此重载才参与重载决议。等价于
return std::uses_allocator_construction_args<T>(alloc,
    std::piecewise_construct,
    std::forward_as_tuple(pr.first),
    std::forward_as_tuple(pr.second)
);
7,8) 仅当 Tstd::pair 的特化时,此重载才参与重载决议。等价于
return std::uses_allocator_construction_args<T>(alloc,
    std::piecewise_construct,
    std::forward_as_tuple(std::get<0>(std::move(pr))),
    std::forward_as_tuple(std::get<1>(std::move(pr)))
);
9) 仅当 Tstd::pair 的特化时,此重载才参与重载决议,并且给定仅用于展示的函数模板
template<class A, class B>
void /*deduce-as-pair*/(const std::pair<A, B>&);

,当作为未求值的操作数时,/*deduce-as-pair*/(non_pair) 是非良构的。
令仅用于展示的类 pair-constructor 定义为

class /*pair-constructor*/
{
    const Alloc& alloc_; // exposition only
    NonPair&     u_;     // exposition only
 
    constexpr reconstruct(const std::remove_cv<T>& p) const // exposition only
    {
        return std::make_obj_using_allocator<std::remove_cv<T>>(alloc_, p);
    }
 
    constexpr reconstruct(std::remove_cv<T>&& p) const // exposition only
    {
        return std::make_obj_using_allocator<std::remove_cv<T>>(alloc_, std::move(p));
    }
 
public:
    constexpr operator std::remove_cv<T>() const
    {
        return reconstruct(std::forward<NonPair>(u_));
    }
};
此重载等价于 return std::make_tuple(pair_construction);,其中 pair_constructionpair-constructor 类型的值,其 alloc_u_ 成员分别为 allocnon_pair

目录

[编辑] 参数

alloc - 要使用的分配器
args - 要传递给 T 构造函数的参数
x - 要传递给 Tfirst 数据成员构造函数的参数元组
y - 要传递给 Tsecond 数据成员构造函数的参数元组
u - 要传递给 Tfirst 数据成员构造函数的单个参数
v - 要传递给 Tsecond 数据成员构造函数的单个参数
pr - 一个 pair,其 first 数据成员将传递给 Tfirst 数据成员的构造函数,其 second 数据成员将传递给 Tsecond 数据成员的构造函数
non_pair - 要转换为 std::pair 以进行进一步构造的单个参数

[编辑] 返回值

适合传递给 T 构造函数的参数 std::tuple

[编辑] 注意

重载 (2-9) 提供分配器传播到 std::pair 中,它不支持前置分配器或后置分配器调用约定(与例如使用前置分配器约定的 std::tuple 不同)。

当在 uses-allocator 构造中使用时,pair-constructor 的转换函数首先将提供的参数转换为 std::pair,然后通过 uses-allocator 构造从该 std::pair 构造结果。

[编辑] 示例

[编辑] 缺陷报告

下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。

缺陷报告 应用于 发布时的行为 正确的行为
LWG 3525 C++20 没有重载可以处理可转换为 pair 的非 pair 类型 添加了重构重载

[编辑] 参阅

检查指定类型是否支持 uses-allocator 构造
(类模板) [编辑]
通过 uses-allocator 构造创建给定类型的对象
(函数模板) [编辑]
通过使用分配器构造在指定内存位置创建给定类型的对象
(函数模板) [编辑]