Boost logo

Boost :

From: George A. Heintzelman (georgeh_at_[hidden])
Date: 2001-10-15 11:20:40


> // Why does
> std::string str1;
> const char *pcc1 = (str1 =
> boost::regex_merge((std::string)"foobar",
> (boost::regex)"o", "uu")).c_str();
>
> // work, but
> const char *pcc2 = ( /* nothing */
> boost::regex_merge((std::string)"foobar",
> (boost::regex)"o","uu")).c_str();
>
> // does not work? IOWs, why is the assignment to a temporary variable
> necessary?

Because the std::string returned by regex_merge is itself a temporary,
and will be destroyed at the end of the statement. It's just like
writing:

const char * hw = string("Hello world").c_str();

Which will also not work.

OTOH:

const string & pcc2 = boost::regex_merge(...);

ought to stay around by the C++ rules on const refs. You can then call
.c_str() on it at your pleasure.

George Heintzelman
georgeh_at_[hidden]


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk