Lambda and map, param by reference - compile error

Viewed 295

I've tried to narrow down my problem to a minimal example:

#include <algorithm>
#include <map>
#include <string>
#include <vector>

int main()
{
    std::vector<int> result;
    std::map<std::string, std::pair<unsigned int, std::vector<int>>> other;

    if (true)
    {
        std::for_each(other.begin(), other.end(),
            [&](std::pair<std::string, std::pair<unsigned int, std::vector<int>>> & data)
            {
                result.insert(result.end(), data.second.second.begin(), data.second.second.end());
            });
    }

    return 0;
}

I get a compiler error:

error C2664: 'void main::<lambda_1b93236899a42921c1aec8d5288e5b90>::operator ()(std::pair<std::string,std::pair<unsigned int,std::vector<int,std::allocator<_Ty>>>> &) const': cannot convert argument 1 from 'std::pair<const _Kty,_Ty>' to 'std::pair<std::string,std::pair<unsigned int,std::vector<int,std::allocator<_Ty>>>> &'

As far as I can tell the lambda parameter is indeed a reference to the type that is contained by the map which we are iterating through. That is the type I am supposed to be using, right?

If I remove the amperstand before data it compiles.

Why?

I do not want to pass each element by value, as those collections will contain a lot of data in my real program.

If I replace the lambda param with auto & it compiles, which leads me to believe the type in the lambda param does not match the type contained by the map, but it sure looks like it does to me. Also, why would the original compile without & if the type is wrong?

What am I missing/not understanding?

2 Answers
Related