Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

371
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda