std::polymorphic<T, Allocator>::polymorphic

来自cppreference.com
< cpp‎ | memory‎ | polymorphic
 
 
内存管理库
(仅用于阐述*)
分配器
未初始化内存算法
受约束的未初始化内存算法
内存资源
未初始化存储 (C++20 前)
(C++17 弃用)
(C++17 弃用)

垃圾收集器支持 (C++23 前)
(C++11)(C++23 前)
(C++11)(C++23 前)
(C++11)(C++23 前)
(C++11)(C++23 前)
(C++11)(C++23 前)
(C++11)(C++23 前)
 
 
constexpr explicit polymorphic();
(1) (C++26 起)
constexpr explicit polymorphic( std::allocator_arg_t, const Allocator& a );
(2) (C++26 起)
template< class U = T >
constexpr explicit polymorphic( U&& v );
(3) (C++26 起)
template< class U = T >

constexpr explicit polymorphic( std::allocator_arg_t, const Allocator& a,

                                U&& v );
(4) (C++26 起)
template< class U, class... Args >
constexpr explicit polymorphic( std::in_place_type_t<U>, Args&&... args );
(5) (C++26 起)
template< class U, class... Args >

constexpr explicit polymorphic( std::allocator_arg_t, const Allocator& a,

                                std::in_place_type_t<U>, Args&&... args );
(6) (C++26 起)
template< class U, class I, class... Args >

constexpr explicit polymorphic( std::in_place_type_t<U>,
                                std::initializer_list<I> ilist,

                                Args&&... args );
(7) (C++26 起)
template< class U, class I, class... Args >

constexpr explicit polymorphic( std::allocator_arg_t, const Allocator& a,
                                std::in_place_type_t<U>,
                                std::initializer_list<I> ilist,

                                Args&&... args );
(8) (C++26 起)
constexpr polymorphic( const polymorphic& other );
(9) (C++26 起)
constexpr polymorphic( std::allocator_arg_t, const Allocator& a,
                       const polymorphic& other );
(10) (C++26 起)
constexpr polymorphic( polymorphic&& other ) noexcept;
(11) (C++26 起)
constexpr polymorphic( std::allocator_arg_t, const Allocator& a,
                       polymorphic&& other ) noexcept(/* see below */);
(12) (C++26 起)

构造新的 polymorphic 对象。

参数

a - 要关联的分配器
v - 用来初始化拥有的值的值
args - 用来初始化拥有的值的实参
il - 用来初始化拥有的值的初始化器列表
other - 另一个 polymorphic 对象,它包含的值(如果存在)会被复制

效果

polymorphic 对象的构造包含以下步骤:

1) 构造关联的分配器 alloc
2) 构造拥有的对象:
  • 对于重载 (1-8),调用 std::allocator_traits<Allocator>::construct(alloc , p, args...),其中:
    • p 是指向对于要构造的拥有的对象合适的存储的 U* 类型指针。
    • args... 是包含各初始化器实参的表达式包。
  • 对于重载 (9-12)
    • 如果 other 无值,那么不会构造拥有的对象,并且 *this 在构造后同样无值。
    • 否则,如果 other 是右值引用,并且 alloc 等于 other.alloc,那么 *this 会获得 other 拥有的对象的所有权。
    • 否则会按上述方法通过 alloc 构造拥有的对象。
  重载   ...的初始化器 拥有的对象的类型  构造后的 valueless_after_move() 
alloc 拥有的对象
(1) (空) (空) T false
(2) a
(3) (空) std::forward<U>(v) U
(4) a
(5) (空) std::forward<Args>(args)
(6) a
(7) (空) ilist,                  
std::forward<Args>(args)
(8) a
(9)  见下文  *other
(仅当 other 有值)
other 拥有的对象的类型
仅当 other 无值时是 true
(10) a
(11) std::move         
    (other.alloc )
获得所有权
(仅当 other 有值)
(12) a 见下文
9) alloc 会以 std::allocator_traits<Allocator>::
    select_on_container_copy_construction(other.alloc )
直接非列表初始化。
12) 拥有的对象会按以下方式初始化:
  • 如果 other 无值,那么 *this 同样无值。
  • 否则,如果 alloc == other.alloctrue,那么 *this 会获取 other 拥有的对象的所有权。
  • 否则通过 alloc*std::move(other) 构造拥有的对象。

约束和补充信息

1,2) 如果以下任意值是 false,那么程序非良构:

1) 此重载只有在 std::is_default_constructible_v<Allocator>true 时才会参与重载决议。

3-8) 这些重载只有在以下所有值都是 true 时才会参与重载决议:
3,4) U
5,6) Args...
7,8) std::initializer_list<I>&, Args...

3,5,7) 这些重载只有在 std::is_default_constructible_v<Allocator>true 时才会参与重载决议。
3,4) 这些重载只有在满足以下所有条件时才会参与重载决议:
5-8) 这些重载只有在 std::is_same_v<std::remove_cvref_t<U>, U>true 时才会参与重载决议。

异常

除非 std::allocator_traits<Allocator>::allocatestd::allocator_traits<Allocator>::construct 抛出异常,否则不会抛出异常。

12)
noexcept 说明:  
noexcept(std::allocator_traits<Allocator>::is_always_equal::value)

示例

参阅

标签类型,用于选择具分配器的构造函数重载
(类)
原位构造标签
(类模板)