Boost logo

Boost Users :

Subject: Re: [Boost-users] [Bind] Unexpected behavior when binding constchar* to function
From: Kim Barrett (kab.conundrums_at_[hidden])
Date: 2010-04-28 21:04:04


On Apr 28, 2010, at 8:22 PM, OvermindDL1 wrote:
> It is (as I recall) defined in the standard that the c_str() member
> returned by an std::string's lifespan is only until the next
> termination point (generally a semicolon in C++).

Not exactly.

21.3.6/2: ... Nor shall the program treat the returned value as a valid pointer value after any subsequent call to a non-const member function of the class basic_string that designates the same object as this.

So anything that can modify the contents of the string (including things like its size and capacity, and also including destruction), and only those things, invalidates the c_str() result.

But the general point that OvermindDL1 and others are making is correct: one needs to be aware of lifetime issues and what the interface one is using specifies in that regard.


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