Topic: mutable can be removed...


Author: Igor Boukanov <Igor.Boukanov@fi.uib.no>
Date: 1996/04/12
Raw View
Subject: mutable can be removed...
Newsgroups: comp.std.c++
Organization: Fysisk institutt, Universitetet i Bergen
Summary: mutable semantic can be realized by template function.
Keywords: C++, mutable

Consider the following:

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

So now instead of:

struct X {
   mutable int i;
   void set(int i_) const;
};
void X::set(int i_) const {
   i = i_;
}

one can wright:

struct X {
   int i;
   void set(int i_) const;
};
void X::set(int i_) const {
   remove_constness_from(this)->i = i_;
}

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

>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!
And of cause this will make C++ standard shorter...

--
Regards, Igor Boukanov.
mailto:igor.boukanov@fi.uib.no
http://www.fi.uib.no/~boukanov/
---
[ 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
]





Author: austern@isolde.mti.sgi.com (Matt Austern)
Date: 1996/04/12
Raw View
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                             ]





Author: kuehl@uzwil.informatik.uni-konstanz.de (Dietmar Kuehl)
Date: 1996/04/12
Raw View
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
]