Boost logo

Boost Users :

Subject: Re: [Boost-users] [Bind] Unexpected behavior when binding const char* to function
From: Michael Caisse (boost_at_[hidden])
Date: 2010-04-27 14:55:25


Piotr Jachowicz wrote:
> Hello,
>
> Igor R <boost.lists <at> gmail.com> writes:
>
>
>>> function<void ()> f = boost::bind(echo, s.c_str());
>>>
>> The expression x.c_str() returns a pointer that becomes invalid as
>> soon as any non-const member function of std::string is called for x.
>>
>
> I know reason behind. But use is right to think "I've bind >>abc<<.
> Binder should store it and use on f() invocation".
>
>

Piotr -

I don't think this is "unexpected behavior" to most. You have *not*
bound abc. You have bound a pointer. What that pointer points to may
change and when you use the pointer it may very well point to something
different than at the time of the bind.

This isn't "unexpected". Your bind is to a pointer. You need to either
ensure that the value pointed at doesn't change or you can pass an object
containing the data itself.

michael

-- 
----------------------------------
Michael Caisse
Object Modeling Designs
www.objectmodelingdesigns.com

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net