7 Dec
2006
7 Dec
'06
4:16 p.m.
roblin@jlab.org 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 );