Topic: mutable question


Author: "Augustin MOGA" <amoga@aspdeveloper.net>
Date: 1997/12/01
Raw View
hi,

i'm wondering why 'mutable' is classified as storage class specifier when
all it does is changing the type of data? in fact, the only collision which
'mutable' may produce is with the 'const' type qualifier -- it is not
possible to declare something like this:

struct A
{
    mutable const int i;
}

or maybe i'm wrong somewhere?

can anyone tell me the reasons behind the promotion of 'mutable' as a new
keyword in C++ standard?

thanks in advance,
AM
---
[ 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: fjh@mundook.cs.mu.OZ.AU (Fergus Henderson)
Date: 1997/12/01
Raw View
"Augustin MOGA" <amoga@aspdeveloper.net> writes:

>i'm wondering why 'mutable' is classified as storage class specifier when
>all it does is changing the type of data?

I think the main reason is that "storage class specifier" happened
to be the right place in the C++ grammar to insert `mutable'.

But `mutable' can determine whether something can be placed in read-only
storage or not, so I guess it can reasonably be considered a storage
class specifier.

>in fact, the only collision which
>'mutable' may produce is with the 'const' type qualifier -- it is not
>possible to declare something like this:
>
>struct A
>{
>    mutable const int i;
>}

That example is ill-formed -- see 7.1.1 [dcl.stc] pararaph 8.

>can anyone tell me the reasons behind the promotion of 'mutable' as a new
>keyword in C++ standard?

The basic idea is to support objects which are logically const,
but which are not bit-wise const.  This occurs when a data member in
an object holds something which is not logically part of the abstract
state of that object (e.g. a cache value).

--
Fergus Henderson <fjh@cs.mu.oz.au>   |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>   |  of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3         |     -- the last words of T. S. Garp.
---
[ 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: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Date: 1997/12/02
Raw View
Augustin MOGA wrote:
>
> hi,
>
> i'm wondering why 'mutable' is classified as storage class specifier when
> all it does is changing the type of data? in fact, the only collision which
> 'mutable' may produce is with the 'const' type qualifier -- it is not
> possible to declare something like this:
>
> struct A
> {
>     mutable const int i;
> }
>
> or maybe i'm wrong somewhere?

The example above shouldn't compile

> can anyone tell me the reasons behind the promotion of 'mutable' as a new
> keyword in C++ standard?

The mutable qualifier allows changing a member variable even if the object
it contains is const:

struct A
{
  int i;
  mutable int j;
  A() {}
};

int main()
{
  A a;
  const A ca;
  a.i=1;  // Ok: a is not const
  ca.i=1; // Error: ca is const
  ca.j=1; // Ok: j is mutable
}

The rational behind it is supporting logical constness, that is, allow
internal changes which don't change the external behaviour.
A typical example is f.ex. caching:

class file
{
  mutable cache file_cache;
  ...
public:
  void read(int pos, void* data, int size) const;  // reading doesn't
change data
  void write(int pos, void* data, int size); // writing does
  ...
};

void file::read(int pos, void* data, int size) const
{
  if(file.cache.valid(pos, size))
    file_cache.get(pos, data, size);
  else
  {
    direct_read(pos, data, size);
    file_cache.set(pos, data, size);
        // would be impossible for non-mutable file_cache, as *this is const
  }
}

int main()
{
  const file config("test.ini"); // readonly file
  int id;
  config.read(5, &id, sizeof(id)); // would not be possible,
       // if read were not const
  ...
}
---
[ 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                             ]