c++


Lambda and map, param by reference - compile error


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 lamda param with auto & it compiles, which leads me to believe the type in the lamda 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?
std::map<Key, T>'s value_type is std::pair<const Key, T>, not std::pair<Key, T>. The version without an ampersand makes a copy of each pair. Since you can copy const Key to Key everything is fine. Add a const to your lambda's parameter type.
#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<const std::string, std::pair<unsigned int, std::vector<int>>> & data)
// Add this const ^^^^^
{
result.insert(result.end(), data.second.second.begin(), data.second.second.end());
});
}
return 0;
}
I've been strugglig with the very same problem today.
I solved that making the key value type in the std::pair const:
[&](std::pair<const std::string, std::pair<unsigned int, std::vector<int>>> & data)
// ^^^^^
After thinking about that a bit, it's quite logical:
You cannot change the key_value of a std::map's entry to something different. So if it's working with an auto loop and a reference it needs to be specified as const.
Make the parameter to your lambda "const".

Related Links

Audacity PCM Data [on hold]
Best practice to set up qt application that works for different DPI
openssl fips error on arm Linux
C++ - Pretty way to make frequencies add up to 1
integerset obtaining the given array
Arduino Invalid conversion from 'char' to 'char*'
virtual ClistCtrl with checkboxes on displayed report list style
Error when modifying vector member of another class
Interrupt Service Routine can't be called directly
Lambda capture by default reference vs named
convert bitset<64> to string every 8 bit
argument of type “int” incompatible with parameter of type “int”
When is the correct time to call restoreState for QHeaderView?
How does it pass lvalue reference to std::forward with CRTP?
Using lower_bound function in loop is giving runtime error?
Problems with rand(), always taking the same random generated number [duplicate]

Categories

HOME
loadrunner
reverse-geocoding
datastax
statistics
kotlin
mongoose
g2o
custom-attributes
uwp-xaml
chart.js
houndify
access-token
raspberry-pi3
graphviz
aurelia-router
distributed-database
outlook-redemption
interaction
log4net
ninja
wmi-query
restcomm
jlabel
pyvmomi
zillow
xlwings
greedy
fgets
osx-elcapitan
postmates
cookie-session
virtuoso
indexof
ply
interceptor
android-logcat
affiliate
kafka-python
hp-quality-center
infowindow
pam
slamdata
aspose-cells
gsutil
submenu
nerdtree
960.gs
watchdog
multibox
media-type
ranorex
xunit.net
audiounit
clisp
virsh
wamp-protocol
proof-of-correctness
case-class
snobol
xml-rpc
android-runtime
svg-edit
countdowntimer
reportviewer2008
raptor
owner
giflib
servant
f#-interactive
mime-mail
irs
integer-overflow
javascript-globalize
payara
jedit
distinct-on
gstat
ekeventstore
savefiledialog
childviewcontroller
scrollviewer
resin
avaloq-script
ember-qunit
aglio
accessdatasource
sharepoint-clientobject
kiwi
invalidation
psychology
rails-i18n
apiaxle
parallel.for
avassetwriter
microsoft-search-server
zebra-puzzle
django-1.2
ubuntu-13.04
system.speech.recognition
angularjs-e2e
rdata
tandem
remotipart
ios5.1
alpha-five
nspasteboard
actinic
cohesion
rails-models
single-user
visual-programming

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile