c++ - Lambda returning a reference causes a segmentation fault - why? -
the following code taken straight out of 1 of projects. first version causes crash (segmentation fault). second version works intended.
aren't 2 code snippets equivalent?
this 1 crashes
auto getuserfrompacket = [&](sf::packet& mp) -> user& { return users.getuser(ssvuj::as<std::string>(getdecompressedpacket(mp), 0)); }; phandler[fromclient::us_death] = [&](clienthandler&, sf::packet& mp) { getuserfrompacket(mp).stats.deaths += 1; // segmentation fault here! };
this 1 works
phandler[fromclient::us_death] = [&](clienthandler&, sf::packet& mp) { users.getuser(ssvuj::as<std::string>(getdecompressedpacket(mp), 0)).stats.deaths += 1; // works fine };
compiler used: clang++ 3.4 - couldn't deduce return type of getuserfrompacket
. users
instance of userdb
. function signature user& userdb::getuser(const std::string&)
- why compiler fail deduce user&
return type?
no, aren't equivalent. first 1 refer lambda, (likely) not in scope time needed. second has no such dependency. careful capturing reference. :)
Comments
Post a Comment