Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ > GotW #88: Is it...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 18 Topic 43061 of 47561
Post > Topic >>

GotW #88: Is it safe to const_cast a reference to a tem****ary?

by Niels Dekker - no return address <unknown@[EMAIL PROTECTED] > Feb 2, 2008 at 10:19 PM

Herb Sutter wrote a new Guru-of-the-Week column last month, GotW #88,
about binding a tem****ary object to a reference-to-const.  Now if this
tem****ary isn't const, is it safe to const_cast the reference?

#include <string>
#include <cassert>
using std::string;

  string f() { return "abc"; } 

  void h() {
    const string& s1 = f();
    const string& s2 = s1;
    const_cast<string&>(s1) = "new value!";  // Safe?
    assert(s1 == "new value!");  // Okay?
    assert(s2 == "new value!");  // Okay?
  }

See also:
GotW #88 - A Candidate For the "Most Im****tant const."
http://herbsutter.spaces.live.com/blog/cns!2D4327CC297151BB!378.entry

Kind regards,
-- 
Niels Dekker
http://www.xs4all.nl/~nd/dekkerware
Scientific programmer at LKEB, Leiden University Medical Center
 




 18 Posts in Topic:
GotW #88: Is it safe to const_cast a reference to a temporary?
Niels Dekker - no return   2008-02-02 22:19:25 
Re: GotW #88: Is it safe to const_cast a reference to a temporar
"Alf P. Steinbach&qu  2008-02-02 22:37:20 
Re: GotW #88: Is it safe to const_cast a reference to a temporar
"Alf P. Steinbach&qu  2008-02-02 22:49:26 
Re: GotW #88: Is it safe to const_cast a reference to a temporar
Niels Dekker - no return   2008-02-03 09:28:14 
Re: GotW #88: Is it safe to const_cast a reference to a temporar
"Alf P. Steinbach&qu  2008-02-03 18:17:52 
Re: GotW #88: Is it safe to const_cast a reference to a temporar
James Kanze <james.kan  2008-02-03 03:39:18 
Re: GotW #88: Is it safe to const_cast a reference to a temporar
"Alf P. Steinbach&qu  2008-02-03 15:55:00 
Re: GotW #88: Is it safe to const_cast a reference to a temporar
James Kanze <james.kan  2008-02-03 10:34:17 
Re: GotW #88: Is it safe to const_cast a reference to a temporar
"Alf P. Steinbach&qu  2008-02-03 20:08:30 
Re: GotW #88: Is it safe to const_cast a reference to a temporar
"Alf P. Steinbach&qu  2008-02-03 20:24:21 
Re: GotW #88: Is it safe to const_cast a reference to a temporar
James Kanze <james.kan  2008-02-04 00:59:25 
Re: GotW #88: Is it safe to const_cast a reference to a temporar
Niels Dekker - no return   2008-02-04 21:01:00 
Re: GotW #88: Is it safe to const_cast a reference to a temporar
"Alf P. Steinbach&qu  2008-02-04 21:19:33 
Re: GotW #88: Is it safe to const_cast a reference to a temporar
"Alf P. Steinbach&qu  2008-02-04 21:26:27 
Re: GotW #88: Is it safe to const_cast a reference to a temporar
Niels Dekker - no return   2008-02-05 23:16:06 
Re: GotW #88: Is it safe to const_cast a reference to a temporar
Niels Dekker - no return   2008-02-06 11:52:51 
Re: GotW #88: Is it safe to const_cast a reference to a temporar
"steady.li@[EMAIL PR  2008-02-04 21:09:50 
Re: GotW #88: Is it safe to const_cast a reference to a temporar
"Alf P. Steinbach&qu  2008-02-05 07:05:35 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sun Sep 7 4:03:21 CDT 2008.