[boost-users][lambda] crash in std::for_each

The following code crashes in for_each when trying to output the 1st pair. What's wrong with it? Thanks! namespace bl = boost::lambda; std::map<std::string, std::string> temp; temp["a"] = "1"; temp["b"] = "2"; const std::map<std::string, std::string> attrs(temp); typedef std::pair<std::string, std::string> string_pair; std::stringstream stream; std::for_each(attrs.begin(), attrs.end(), stream << bl::bind(&string_pair::first, bl::_1) << bl::bind(&string_pair::second, bl::_1));

On Tue, Feb 3, 2009 at 9:45 AM, Igor R <boost.lists@gmail.com> wrote:
The following code crashes in for_each when trying to output the 1st pair. What's wrong with it?
Thanks!
namespace bl = boost::lambda; std::map<std::string, std::string> temp; temp["a"] = "1"; temp["b"] = "2"; const std::map<std::string, std::string> attrs(temp); typedef std::pair<std::string, std::string> string_pair; std::stringstream stream; std::for_each(attrs.begin(), attrs.end(), stream << bl::bind(&string_pair::first, bl::_1) << bl::bind(&string_pair::second, bl::_1));
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
What's the problem? This works for me - full source follows. #include "boost/lambda/lambda.hpp" #include "boost/lambda/bind.hpp" #include <algorithm> #include <map> #include <iostream> #include <sstream> int main( ) { namespace bl = boost::lambda; std::map<std::string, std::string> temp; temp["a"] = "1"; temp["b"] = "2"; const std::map<std::string, std::string> attrs(temp); typedef std::pair<std::string, std::string> string_pair; std::stringstream stream; std::for_each(attrs.begin(), attrs.end(), stream << bl::bind(&string_pair::first, bl::_1) << bl::bind(&string_pair::second, bl::_1)); } Using Visual Studio 2005 & Boost 1.36 Rob. --

Oh no it doesn't ! Please ignore my last post. On Tue, Feb 3, 2009 at 12:29 PM, Robert Jones <robertgbjones@gmail.com>wrote:
On Tue, Feb 3, 2009 at 9:45 AM, Igor R <boost.lists@gmail.com> wrote:
The following code crashes in for_each when trying to output the 1st pair. What's wrong with it?
Thanks!
namespace bl = boost::lambda; std::map<std::string, std::string> temp; temp["a"] = "1"; temp["b"] = "2"; const std::map<std::string, std::string> attrs(temp); typedef std::pair<std::string, std::string> string_pair; std::stringstream stream; std::for_each(attrs.begin(), attrs.end(), stream << bl::bind(&string_pair::first, bl::_1) << bl::bind(&string_pair::second, bl::_1));
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
What's the problem? This works for me - full source follows.
#include "boost/lambda/lambda.hpp" #include "boost/lambda/bind.hpp" #include <algorithm> #include <map> #include <iostream> #include <sstream>
int main( ) { namespace bl = boost::lambda; std::map<std::string, std::string> temp; temp["a"] = "1"; temp["b"] = "2"; const std::map<std::string, std::string> attrs(temp); typedef std::pair<std::string, std::string> string_pair; std::stringstream stream; std::for_each(attrs.begin(), attrs.end(), stream << bl::bind(&string_pair::first, bl::_1) << bl::bind(&string_pair::second, bl::_1)); }
Using Visual Studio 2005 & Boost 1.36
Rob.
--
-- ACCU - Professionalism in programming - http://www.accu.org

On Tue, Feb 3, 2009 at 9:45 AM, Igor R <boost.lists@gmail.com> wrote:
The following code crashes in for_each when trying to output the 1st pair. What's wrong with it?
Thanks!
namespace bl = boost::lambda; std::map<std::string, std::string> temp; temp["a"] = "1"; temp["b"] = "2"; const std::map<std::string, std::string> attrs(temp); typedef std::pair<std::string, std::string> string_pair; std::stringstream stream; std::for_each(attrs.begin(), attrs.end(), stream << bl::bind(&string_pair::first, bl::_1) << bl::bind(&string_pair::second, bl::_1));
Try changing the string_pair typedef to: std::pair<std::string const, std::string> string_pair; Not 100% sure *why* this makes it run rather than crash (with VC++ 2008, Boost 1.37.0), but it makes some sort of sense, as the key in a map is immutable. Stuart Dootson

AMDG Stuart Dootson wrote:
Try changing the string_pair typedef to:
std::pair<std::string const, std::string> string_pair;
Not 100% sure *why* this makes it run rather than crash (with VC++ 2008, Boost 1.37.0), but it makes some sort of sense, as the key in a map is immutable.
The reason is that std::pairs can be converted to each other. If the type doesn't match exactly: 1) It will be implicitly converted to the correct type. 2) Then you get a reference to first. 3) Then the temporary created in step 1 is destroyed 4) Then you stream first (which has just been destroyed) In Christ, Steven Watanabe

The reason is that std::pairs can be converted to each other. If the type doesn't match exactly: 1) It will be implicitly converted to the correct type. 2) Then you get a reference to first. 3) Then the temporary created in step 1 is destroyed 4) Then you stream first (which has just been destroyed)
Thank you, now I see.

On Tue, Feb 3, 2009 at 5:00 PM, Steven Watanabe <watanabesj@gmail.com> wrote:
AMDG
Stuart Dootson wrote:
Try changing the string_pair typedef to:
std::pair<std::string const, std::string> string_pair;
Not 100% sure *why* this makes it run rather than crash (with VC++ 2008, Boost 1.37.0), but it makes some sort of sense, as the key in a map is immutable.
The reason is that std::pairs can be converted to each other. If the type doesn't match exactly: 1) It will be implicitly converted to the correct type. 2) Then you get a reference to first. 3) Then the temporary created in step 1 is destroyed 4) Then you stream first (which has just been destroyed)
In Christ, Steven Watanabe
Thanks Steven - it's always good to get some educashun! :-) Stuart D

On Tue, Feb 3, 2009 at 9:45 AM, Igor R <boost.lists@gmail.com> wrote:
The following code crashes in for_each when trying to output the 1st pair. What's wrong with it?
Thanks!
namespace bl = boost::lambda; std::map<std::string, std::string> temp; temp["a"] = "1"; temp["b"] = "2"; const std::map<std::string, std::string> attrs(temp); typedef std::pair<std::string, std::string> string_pair; std::stringstream stream; std::for_each(attrs.begin(), attrs.end(), stream << bl::bind(&string_pair::first, bl::_1) << bl::bind(&string_pair::second, bl::_1));
Or essentially the same solution, but tidied up
#include "boost/lambda/lambda.hpp" #include "boost/lambda/bind.hpp" #include <algorithm> #include <map> #include <iostream> #include <sstream> int main( ) { namespace bl = boost::lambda; typedef std::map<std::string, std::string> map_type; map_type temp; temp["a"] = "1"; temp["b"] = "2"; const map_type attrs(temp); typedef map_type :: value_type string_pair; std::stringstream stream; std::for_each(attrs.begin(), attrs.end(), stream << bl::bind(&string_pair::first, bl::_1) << bl::bind(&string_pair::second, bl::_1)); } Cheers, Rob.
participants (4)
-
Igor R
-
Robert Jones
-
Steven Watanabe
-
Stuart Dootson