std::from_range, std::from_range_t
来自 cppreference.cn
| 定义于头文件 <ranges> |
||
| struct from_range_t { explicit from_range_t() = default; }; |
(C++23 起) | |
| inline constexpr std::from_range_t from_range {}; |
(C++23 起) | |
std::from_range 是一个消歧标记,可以传递给合适容器的构造函数,以指示包含的成员是通过范围构造的。
相应的类型 std::from_range_t 可以用在构造函数的参数列表中,以匹配预期的标记。
目录 |
[编辑] 标准库
以下标准库类型在其构造函数中使用 std::from_range_t 类型
容器库 | |
| (C++23) |
从范围构造 vector( std::vector<T,Allocator> 的公共成员函数)
|
| (C++26) |
从范围构造 inplace_vector( std::inplace_vector<T,N> 的公共成员函数)
|
| (C++23) |
从范围构造 deque( std::deque<T,Allocator> 的公共成员函数)
|
| (C++23) |
从范围构造 forward_list( std::forward_list<T,Allocator> 的公共成员函数)
|
| (C++23) |
从范围构造 list( std::list<T,Allocator> 的公共成员函数)
|
| (C++23) |
从范围构造 set( std::set<Key,Compare,Allocator> 的公共成员函数)
|
| (C++23) |
从范围构造 map( std::map<Key,T,Compare,Allocator> 的公共成员函数)
|
| (C++23) |
从范围构造 multiset( std::multiset<Key,Compare,Allocator> 的公共成员函数)
|
| (C++23) |
从范围构造 multimap( std::multimap<Key,T,Compare,Allocator> 的公共成员函数)
|
| (C++23) |
从范围构造 unordered_set( std::unordered_set<Key,Hash,KeyEqual,Allocator> 的公共成员函数)
|
| (C++23) |
从范围构造 unordered_map( std::unordered_map<Key,T,Hash,KeyEqual,Allocator> 的公共成员函数)
|
| (C++23) |
从范围构造 unordered_multiset( std::unordered_multiset<Key,Hash,KeyEqual,Allocator> 的公共成员函数)
|
| (C++23) |
从范围构造 unordered_multimap( std::unordered_multimap<Key,T,Hash,KeyEqual,Allocator> 的公共成员函数)
|
| (C++23) |
从范围构造 priority_queue( std::priority_queue<T,Container,Compare> 的公共成员函数)
|
| (C++23) |
从范围构造 queue( std::queue<T,Container> 的公共成员函数)
|
| (C++23) |
从范围构造 stack( std::stack<T,Container> 的公共成员函数)
|
| (C++23) |
从范围构造 flat_set( std::flat_set<Key,Compare,KeyContainer> 的公共成员函数)
|
| (C++23) |
从范围构造 flat_map( std::flat_map<Key,T,Compare,KeyContainer,MappedContainer> 的公共成员函数)
|
| (C++23) |
从范围构造 flat_multiset( std::flat_multiset<Key,Compare,KeyContainer> 的公共成员函数)
|
| (C++23) |
从范围构造 flat_multimap( std::flat_multimap<Key,T,Compare,KeyContainer,MappedContainer> 的公共成员函数)
|
字符串库 | |
| (C++23) |
从范围构造 basic_string( std::basic_string<CharT,Traits,Allocator> 的公共成员函数)
|
[编辑] 注意
| 特性测试宏 | 值 | 标准 | 特性 |
|---|---|---|---|
__cpp_lib_containers_ranges |
202202L |
(C++23) | 带有标记的构造函数,用于从容器兼容范围构造 |
[编辑] 示例
运行此代码
#include <cassert> #include <string> int main() { #ifdef __cpp_lib_containers_ranges auto const range = {0x43, 43, 43}; std::string str{std::from_range, range}; // uses tagged constructor assert(str == "C++"); #endif }
[编辑] 参阅
| 原地构造标签 (tag) | |
| 表示范围的元素已排序(不要求唯一性) (tag) | |
| (C++23) |
表示范围的元素已排序且唯一 (tag) |
| (C++23) |
从输入范围构造新的非视图对象 (函数模板) |