https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86852

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Michael Veksler from comment #0)
> 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 is a ridiculous example, and I see no advantage to supporting it.

Ideally it would work as:

std::unordered_map m{{1,2}, {3,4}};

Having to write std::initializer_list<std::pair<const int, int>> is ridiculous,
and giving the std::pair type explicitly isn't much better. You might as well
just avoid argument deduction entirely at that point:

std::unordered_map<int, int> m{{1,2}, {3,4}};

The point of class template argument deduction is to simplify code, not so you
can make it far more verbose.

> The only way to make this work in gcc is
>    std::unordered_map m{std::pair<int, int>{1,2}, {3,4}};

You can deduce the pair type:

std::unordered_map m{std::pair{1,2}, {3,4}};

Reply via email to