Boost logo

Boost Users :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2006-12-07 11:16:12


roblin_at_[hidden] wrote:
> Hi all,
> I was trying to understand the use of boost::ref and boost::cref and
> wrote this little snippet:

[...]

> boost::reference_wrapper<string const> ref=boost::cref(string("yoh"));

The string( "yoh" ) temporary is destroyed at the ; and you
reference_wrapper ends up dangling.

A real reference

string const & ref = string("yoh");

is special-cased by the language to prevent the destruction of the temporary
string, but reference_wrapper cannot do so. You need to initialize it with a
real object, not a temporary.

string yoh( "yoh" );
 boost::reference_wrapper<string const> ref = boost::cref( yoh );


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