std::unordered_map の推論ガイド
| ヘッダ <unordered_map> で定義 |
||
| template< class InputIt, class Hash = std::hash<iter_key_t<InputIt>>, |
(1) | (C++17以降) |
| template< class Key, class T, class Hash = std::hash<Key>, class Pred = std::equal_to<Key>, |
(2) | (C++17以降) |
| template< class InputIt, class Alloc > unordered_map( InputIt, InputIt, typename /*下記参照*/::size_type, Alloc ) |
(3) | (C++17以降) |
| template< class InputIt, class Alloc > unordered_map( InputIt, InputIt, Alloc ) |
(4) | (C++17以降) |
| template< class InputIt, class Hash, class Alloc > unordered_map( InputIt, InputIt, typename /*下記参照*/::size_type, Hash, Alloc ) |
(5) | (C++17以降) |
| template< class Key, class T, typename Alloc > unordered_map( std::initializer_list<std::pair<Key, T>>, |
(6) | (C++17以降) |
| template< class Key, class T, typename Alloc > unordered_map( std::initializer_list<std::pair<Key, T>>, Alloc ) |
(7) | (C++17以降) |
| template< class Key, class T, class Hash, class Alloc > unordered_map( std::initializer_list<std::pair<Key, T>>, |
(8) | (C++17以降) |
| template< ranges::input_range R, class Hash = std::hash<range_key_t<R>>, |
(9) | (C++23から) |
| template< ranges::input_range R, class Alloc > unordered_map( std::from_range_t, R&&, |
(10) | (C++23から) |
| template< ranges::input_range R, class Alloc > unordered_map( std::from_range_t, R&&, Alloc ) |
(11) | (C++23から) |
| template< ranges::input_range R, class Hash, class Alloc > unordered_map( std::from_range_t, R&&, typename /* 説明を参照 */::size_type, |
(12) | (C++23から) |
| Exposition-only helper type aliases |
||
| template< class InputIter > using iter_val_t = |
(説明用*) | |
| template< class InputIter > using iter_key_t = |
(説明用*) | |
| template< class InputIter > using iter_mapped_t = |
(説明用*) | |
| template< class InputIter > using iter_to_alloc_t = |
(説明用*) | |
| template< ranges::input_range Range > using range_key_t = |
(C++23から) (説明用*) |
|
| template< ranges::input_range Range > using range_mapped_t = |
(C++23から) (説明用*) |
|
| template< ranges::input_range Range > using range_to_alloc_t = |
(C++23から) (説明用*) |
|
unordered_map がイテレータ範囲(オーバーロード (1,3-5))および std::initializer_list(オーバーロード (2,6-8))から推論できるように提供されています。これらのオーバーロードは、InputIt が LegacyInputIterator を満たし、Alloc が Allocator を満たし、Hash と Pred のどちらも Allocator を満たさず、Hash が整数型でない場合にのみオーバーロード解決に参加します。
注意: ライブラリが型を LegacyInputIterator として満たさないと判断する程度は未規定ですが、最低限、整数型は入力イテレータとして適格ではありません。同様に、型が Allocator を満たさないと判断する程度は未規定ですが、最低限、メンバ型 Alloc::value_type が存在し、式 std::declval<Alloc&>().allocate(std::size_t{}) が評価されないオペランドとして適切であることが必要です。
これらのガイドにおける size_type パラメータ型は、推論ガイドによって推論される型の size_type メンバ型を参照します。
[編集] 注釈
| 機能テストマクロ | 値 | 規格 | 機能 |
|---|---|---|---|
__cpp_lib_containers_ranges |
202202L |
(C++23) | 範囲対応の構築と挿入。オーバーロード (9-12) |
[編集] 例
#include <unordered_map> int main() { // std::unordered_map m1 = {{"foo", 1}, {"bar", 2}}; // Error: braced-init-list has no type cannot // deduce pair<Key, T> from {"foo", 1} or {"bar", 2} std::unordered_map m1 = {std::pair{"foo", 2}, {"bar", 3}}; // guide #2 std::unordered_map m2(m1.begin(), m1.end()); // guide #1 }
[編集] 不具合報告
以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。
| DR | 適用対象 | 公開された動作 | 正しい動作 |
|---|---|---|---|
| LWG 3025 | C++17 | initializer-listガイドは std::pair<const Key, T> を受け取ります | std::pair<Key, T> を使用してください |