🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #4013

Re: mutable can be removed...

#4013
From: Matt Austern
Date: Fri, 12 Apr 1996 16:53
42 lines
2494 bytes
In article <Pine.HPP.3.91.960412122220.29403A-100000@kvark.fi.uib.no> Igor Boukanov <Igor.Boukanov@fi.uib.no> writes:

> template<class T> T* remove_constness_from(const T* p)
> {
>    return const_cast<T*>(p);
> }

...

> And remove_constness_from is more useful than mutable because in this 
> case to make any class member looks like mutable one don't even need to 
> modify class definition! And of cause lines like 
> "remove_constness_from(this)->i = i_;"
> will show explicitly what somebody does...

It's true that you can use const_cast to modify a member of an object
pointed to by a const pointer.  In fact, you don't even need
const_cast: you can cast away constness using old-style casts too.  In
a sense, then, mutable doesn't add new capabilities.  So why was it
added?

Two reasons.  First, you can't safely cast away constness of a const
object; the only case where it's safe is when you have a non-const
object that you're referring to by a const pointer or reference.
Second, the fact that a certain member might change even in const
operations (and even when the object itself is const) is important
enough that it's good to have a notation where that fact can be
specified in the class declaration.  Using const_cast, by contrast,
forces people to look through all of the code that uses that object if
they want to discover this fact.
-- 
Matt Austern
SGI: MTI Compilers Group
austern@isolde.mti.sgi.com



[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
Technical Details
Message-ID: <AUSTERN.96Apr12094908@isolde.mti.sgi.com>
Path: arc1-ca.pugleaf.net!engnews1.Eng.Sun.COM!taumet!clamage
References: <Pine.HPP.3.91.960412122220.29403A-100000@kvark.fi.uib.no>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!engnews1.Eng.Sun.COM!taumet!clamage
From: austern@isolde.mti.sgi.com (Matt Austern)
Newsgroups: comp.std.c++
Subject: Re: mutable can be removed...
Date: 12 Apr 1996 16:53:28 GMT
Organization: SGI
Approved: clamage@eng.sun.com (comp.std.c++)
Message-ID: <AUSTERN.96Apr12094908@isolde.mti.sgi.com>
References: <Pine.HPP.3.91.960412122220.29403A-100000@kvark.fi.uib.no>
Reply-To: austern@mti.mti.sgi.com
NNTP-Posting-Host: taumet.eng.sun.com
X-Nntp-Posting-Host: isolde.mti.sgi.com
In-Reply-To: Igor Boukanov's message of 12 Apr 1996 08:53:37 PDT
Content-Length: 1426
X-Lines: 35
Originator: clamage@taumet