Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

364
Views
What's the reason compiler can't deduce the type on std::for_each with a lambda memberfucntion in std::unordered_map

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>();
    });   
    
}
over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!