🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: mutable can be removed...

#4016
From: Dietmar Kuehl
Date: Fri, 12 Apr 1996 18:07
67 lines
3352 bytes
Hi,
Igor Boukanov (Igor.Boukanov@fi.uib.no) wrote:

[example how to potentially simulate 'mutable' removed]

: 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...

This is why 'const_cast' was invented but it does not replace
'mutable'.  See dcl.type.cv section 4: "Except that any class member
declared mutable can be modified,  any  attempt  to  modify  a  const
object  during its lifetime results in undefined behavior."

Thus, the following would result in undefined behavior:

  class f
  {
    int count_;
  public:
    void count() const
    { 
      const_cast<f*>(this)->count_++;
    }
  };

  f const const_f;

  int main() { const_f.count(); return 0; }

If 'count_' is declare to be 'mutable', everything is fine. Now you
could try to argue, that it would be necessary to remove decl.type.cv
section 4 or rephrase it to make the above code well behaved. But this
would prevent the ability to put 'const' objects into write-protected
memory (like ROM) or to determine their value at compile-time.

Thus, 'mutable' is necessary and it has indeed some useful
applications.  In particular, it allows to implement classes which
maintain logical constness (instead of bitwise constness) in a
convenient way. The fact that the bits of some 'const' object will be
changed is already obvious from the (concious) declaration of the
'mutable' member.

: >From this point of view mutable keyword can be removed from C++. And it 
: will not break any real code because as I now there is no compiler that 
: can compile such code!

g++ implements 'mutable' and removing 'mutable' from the standard would
break my code.  Whether you consider this code to be "real" code is a
different question...

: And of cause this will make C++ standard shorter...

... and less flexible.
--
dietmar.kuehl@uni-konstanz.de
http://www.informatik.uni-konstanz.de/~kuehl/
I am a realistic optimist - that's why I appear to be slightly pessimistic
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your 
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu 
]
Technical Details
Message-ID: <4km58m$189@news.BelWue.DE>
Path: arc1-ca.pugleaf.net!fido.asd.sgi.com!austern
References: <Pine.HPP.3.91.960412122220.29403A-100000@kvark.fi.uib.no>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!fido.asd.sgi.com!austern
From: kuehl@uzwil.informatik.uni-konstanz.de (Dietmar Kuehl)
Newsgroups: comp.std.c++
Subject: Re: mutable can be removed...
Date: 12 Apr 1996 11:07:19 PDT
Organization: Fakultät für Mathematik und Informatik
Approved: austern@isolde.mti.sgi.com
Message-ID: <4km58m$189@news.BelWue.DE>
References: <Pine.HPP.3.91.960412122220.29403A-100000@kvark.fi.uib.no>
Reply-To: dietmar.kuehl@uni-konstanz.de
NNTP-Posting-Host: isolde.mti.sgi.com
X-Original-Date: 12 Apr 1996 17:52:22 GMT
X-Newsreader: TIN [version 1.2 PL2]
X-Auth: PGPMoose V1.1 PGP comp.std.c++
 iQBVAwUBMW6b2Uy4NqrwXLNJAQHqGQH/aaT93Qq/K/pXxl5tDwa5VZjjWfpQWrY1
 5Hz5DDWisMfVW1XWHficU6KtZPFbvs3mP0a16fD4QmIJgEEQvgnIdA==
 =ZCDs
Originator: austern@isolde.mti.sgi.com