std::queue
的推导指引
在标头
<queue>
定义
|
||
template<class
Container>
queue(Container) |
(1) | (C++17 起) |
template<class
InputIt>
queue(InputIt,
InputIt) |
(2) | (C++23 起) |
template<class
Container, class
Alloc>
queue(Container,
Alloc) |
(3) | (C++17 起) |
template<class
InputIt, class
Alloc>
queue(InputIt,
InputIt, Alloc) |
(4) | (C++23 起) |
为 queue
提供推导指引以允许从底层容器类型推导。
这些重载只有在
-
InputIt
(若存在)满足老式输入迭代器 (LegacyInputIterator) , -
Container
(若存在)不满足分配器 (Allocator) , - 对于 (3)
(C++23
前)(4)
(C++23
起)
,
Alloc
满足分配器 (Allocator) ,以及 - 若
Container
与Alloc
均存在则 std::uses_allocator_v<Container, Alloc> 为 true
时才会参与重载决议。
注意:库确定类型是否满足老式输入迭代器 (LegacyInputIterator)
的程度是未指定的,除了最低要求是整数类型不具备输入迭代器的条件。类似地,确定类型是否满足分配器 (Allocator)
是未指定的,除了最低要求是成员类型 Alloc::value_type
必须存在,且表达式 std::declval<Alloc&>().allocate(std::size_t{})
在作为不求值操作数时必须为良构。
示例
#include <vector> #include <queue> int main() { std::vector<int> v = {1,2,3,4}; std::queue s{v}; // 指引 #1 推导 std::queue<int, vector<int>> }