I have read about deduction guides for std::unordered_map
in C++17 from using a cppreference.
Then tried to run following example, which is copied from cppreference.
#include <unordered_map>
int main() {
// std::unordered_map m1 = {{"foo", 1}, {"bar", 2}}; // Error: braced-init-list has no type
// cannot deduce pair<const Key, T> from
// {"foo", 1} or {"bar", 2}
std::unordered_map m1 = std::initializer_list<
std::pair<char const* const, int>>({{"foo", 2}, {"bar", 3}}); // guide #2
std::unordered_map m2(m1.begin(), m1.end()); // guide #1
}
But, the compiler gives an error.
main.cpp: In function 'int main()':
main.cpp:7:84: error: class template argument deduction failed:
std::pair<char const* const, int>>({{"foo", 2}, {"bar", 3}}); // guide #2
^
main.cpp:7:84: error: no matching function for call to 'unordered_map(std::initializer_list<std::pair<const char* const, int> >)'
In file included from /usr/local/include/c++/7.2.0/unordered_map:48:0,
from main.cpp:1:
/usr/local/include/c++/7.2.0/bits/unordered_map.h:101:11: note: candidate: template<class _Key, class _Tp, class _Hash, class _Pred, class _Alloc> unordered_map(std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>)-> std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>
class unordered_map
^~~~~~~~~~~~~
Why does compiler give an error?