I tried to narrow 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 really a reference to the type that is contained in the map that we iterate. This is the type I should use, right?
If I delete the amp, before datait compiles.
Why?
I do not want to pass each element by value, since these collections will contain a lot of data in my real program.
If I replace the lambda parameter with auto &, it compiles, which makes me think that the type in the lambda parameter does not match the type contained in the map, but it seems to be like me. Also, why is the original compiler without and if the type is wrong?
What am I missing / do not understand?