std::stack
的推导指南
来自 cppreference.cn
定义于头文件 <stack> |
||
template< class Container > stack( Container ) |
(1) | (since C++17) |
template< class Container, class Alloc > stack( Container, Alloc ) |
(2) | (since C++17) |
template< class InputIt > stack( InputIt, InputIt ) |
(3) | (since C++23) |
template< class InputIt, class Alloc > stack( InputIt, InputIt, Alloc ) |
(4) | (since C++23) |
template< ranges::input_range R > stack( std::from_range_t, R&& ) |
(5) | (since C++23) |
template< ranges::input_range R, class Allocator > stack( std::from_range_t, R&&, Allocator ) |
(6) | (since C++23) |
这些推导指南为 stack
提供,以允许从底层容器类型进行推导。
1) 从参数推导底层容器类型。
2) 与 (1) 相同,除了提供了分配器。
3) 从迭代器推导元素类型,使用 std::deque<typename std::iterator_traits<InputIt>::value_type> 作为底层容器类型。
4) 与 (3) 相同,除了提供了分配器。
6) 与 (5) 相同,除了提供了分配器。
这些重载仅在以下情况下参与重载决议:
-
InputIt
(如果存在)满足 LegacyInputIterator, -
Container
(如果存在)不满足 Allocator, - 对于 (3)(直到 C++23)(4)(自 C++23 起),
Alloc
满足 Allocator,并且 - std::uses_allocator_v<Container, Alloc> 如果
Container
和Alloc
都存在,则为 true。
注意:库确定类型不满足 LegacyInputIterator 的程度是未指定的,但至少整数类型不符合输入迭代器的条件。同样,它确定类型不满足 Allocator 的程度也是未指定的,但至少成员类型 Alloc::value_type
必须存在,并且当作为未评估的操作数处理时,表达式 std::declval<Alloc&>().allocate(std::size_t{}) 必须是良构的。
[编辑] 注释
特性测试 宏 | 值 | Std | 特性 |
---|---|---|---|
__cpp_lib_adaptor_iterator_pair_constructor |
202106L |
(C++23) | std::queue 和 std::stack 的迭代器对构造函数;重载 (2) 和 (4) |
__cpp_lib_containers_ranges |
202202L |
(C++23) | 范围感知的构造和插入;重载 (5) 和 (6) |
[编辑] 示例
运行此代码
#include <stack> #include <vector> int main() { std::vector<int> v = {1, 2, 3, 4}; std::stack s{v}; // guide #1 deduces std::stack<int, vector<int>> }