Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

366
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda