https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86852
Bug ID: 86852
Summary: map and unordered_map wrong deduction guides for
inilializer_list
Product: gcc
Version: 9.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: libstdc++
Assignee: unassigned at gcc dot gnu.org
Reporter: mickey.veksler at gmail dot com
Target Milestone: ---
According to
https://en.cppreference.com/w/cpp/container/unordered_map/deduction_guides
Deduction guides for unodrered_map:
template<class Key, class T, class Hash = std::hash<Key>,
class Pred = std::equal_to<Key>,
class Alloc = std::allocator<std::pair<const Key, T>>>
unordered_map(std::initializer_list<std::pair<const Key, T>>,
typename /*see below*/::size_type = /*see below*/,
Hash = Hash(), Pred = Pred(), Alloc = Alloc())
-> unordered_map<Key, T, Hash, Pred, Alloc>;
Note that the guide is for std::pair<const Key, T>, i.e., the key is const.
In libstdc++'s unodered_map:
unordered_map(initializer_list<pair<_Key, _Tp>>,
typename unordered_map<int, int>::size_type = {},
_Hash = _Hash(), _Pred = _Pred(), _Allocator = _Allocator())
-> unordered_map<_Key, _Tp, _Hash, _Pred, _Allocator>;
Note that pair<_Key, _Tp>, i.e., the key is not const.
This breaks unordered_map and map deduction guides:
#include <unordered_map>
#include <initializer_list>
int main() {
std::unordered_map m1(std::initializer_list<
std::pair<const int, int>>({{1, 2}, {2, 3}}));
}
This fails with:
<source>: In function 'int main()':
<source>:5:69: error: class template argument deduction failed:
std::pair<const int, int>>({{1, 2}, {2, 3}}));
However, if const is removed from the key:
This breaks unordered_map and map deduction guides:
#include <unordered_map>
#include <initializer_list>
int main() {
std::unordered_map m1(std::initializer_list<
std::pair< int, int>>({{1, 2}, {2, 3}}));
}
Deduction guide works, but it is unusable:
<source>: In function 'int main()':
<source>:5:64: error: no matching function for call to 'std::unordered_map<int,
int>::unordered_map(std::initializer_list<std::pair<int, int> >)'
std::pair< int, int>>({{1, 2}, {2, 3}}));
=====================
The only way to make this work in gcc is
std::unordered_map m{std::pair<int, int>{1,2}, {3,4}};
But this does not seem to be correct.