During work I endountered a problem with a for_each loop.
I have a std::unordered_map
inside a struct which holds another object. When I use std::for_each
i can't use auto&
to call the current element, the compiler can't deduce the type. On the other hand if I avoid auto& and use std::unordered_map<std::size_t, Foo>::value_type&
everything works as expected.
I'm fine with that, but i don't understand why the compiler can't deduce the type here... you can find this example from below here on compiler explorer: https://godbolt.org/z/3eehKsxs7
#include <iostream>
#include <unordered_map>
#include <algorithm>
struct Foo {
template<typename T>
T do_some_work()
{
// do some work ..
T t;
return t;
}
};
struct map_manager {
std::unordered_map<std::size_t, Foo> m_map;
};
int main()
{
Foo f1;
map_manager mm;
mm.m_map[1] = f1;
mm.m_map[2] = f1;
mm.m_map[3] = f1;
std::for_each(std::begin(mm.m_map), std::end(mm.m_map),
//[](std::unordered_map<std::size_t, Foo>::value_type& current){ // works
[](auto& current){ // doesn't work
auto bar = current.second.do_some_work<int>();
});
}