If I try to write a function that is restricted by a concept to either std::map
and std::unordered_map
with Key
keys and Element
elements it fails to deduce the Key
type in the following piece of code.
template<typename A, typename B>
concept is_same_as = std::is_same<A, B>::value;
template<typename T, typename Key, typename Element>
concept map_type_of =
is_same_as<T, std::map<Key, Element>> ||
is_same_as<T, std::unordered_map<Key, Element>>;
template <typename T, typename Key, typename Element>
requires map_type_of<T,Key,Element>
inline std::list<Key> map_extract_keys(T const& a) {
std::list<Key> output;
for (auto const& element : a) {
output.push_back(element.first);
}
return output;
}
std::map<std::string, int> map_example;
auto keys = map_extract_keys(map_example);
The actual error it gives:
candidate: ‘template<class T, class Key, class Element> requires map_type_of<T, Key, Element> std::__cxx11::list<Key> map_extract_keys(const T&)’
inline std::list<Key> map_extract_keys(T const& a) {
note: template argument deduction/substitution failed:
note: couldn’t deduce template parameter ‘Key’
Is there any way to allow that automatic deduction?
Your
map_type_of
concept doesn't help you over a simpler conceptWhich, as a bonus, allows things like
boost::bimap<...>::left_map
etc