Topic: [Q] addess of mutable member in a constant object


Author: wmm@fastdial.net
Date: 1999/10/01
Raw View
In article <B406D318-18A153@192.168.1.8>,
  "Gabor Greif" <gabor@no.netopia.com> wrote:
>
> Consider the following snippet:
>
> struct Ooops
> {
>  mutable int i;
>  Ooops(void) : i(42) { }
> };
>
> int main(void)
> {
>  const Ooops o;
>
>  int& ii(o.i); // hey it works! :-) -- but is it legal?
> }
>
> My compiler accepts it (makes me happy), but is it allowed by the standard
> to get a non-const reference from a mutable member inside of a const
> object? If it is allowed, where can I find it written down? So far I have
> found nothing :-(

If you can get through all the "vq"s and "cq"s, 5.2.5p4 makes it
pretty clear that "o.i" in your example is not const-qualified,
so the reference binding is fine.
--
William M. Miller, wmm@fastdial.net


Sent via Deja.com http://www.deja.com/
Before you buy.


[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Ross Smith <ross.s@ihug.co.nz>
Date: 1999/10/01
Raw View
Gabor Greif wrote:
>
> My compiler accepts it (makes me happy), but is it allowed by the standard
> to get a non-const reference from a mutable member inside of a const
> object? If it is allowed, where can I find it written down? So far I have
> found nothing :-(

Yes, it's supposed to work that way.

7.1.1 Storage class specifiers: "The mutable specifier on a class data
member nullifies a const specifier applied to the containing class
object and permits modification of the mutable class member even though
the rest of the object is const."

7.1.5.1 The cv-qualifiers: "Except that any class member declared
mutable can be modified, any attempt to modify a const object during its
lifetime results in undefined behavior."

--
Ross Smith <ross.s@ihug.co.nz> The Internet Group, Auckland, New Zealand
========================================================================
  "There are many technical details that make Linux attractive to the
  sort of people to whom technical details are attractive."   -- Suck
---
[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Hyman Rosen <hymie@prolifics.com>
Date: 1999/10/01
Raw View
"Gabor Greif" <gabor@no.netopia.com> writes:
> My compiler accepts it (makes me happy), but is it allowed by the standard
> to get a non-const reference from a mutable member inside of a const
> object? If it is allowed, where can I find it written down? So far I have
> found nothing :-(

In 3.9.3/3, it says "Each non-static, non-mutable, non-reference
data member of a const-qualified class object is const-qualified".
So mutable members of const objects are not const-qualified.
---
[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Valentin Bonnard <Bonnard.V@wanadoo.fr>
Date: 1999/10/01
Raw View
Gabor Greif wrote:
> struct Ooops
> {
>         mutable int i;
>         Ooops(void) : i(42) { }
> };
>
> int main(void)
> {
>         const Ooops o;
>
>         int& ii(o.i);   // hey it works! :-) -- but is it legal?
> }
>
> My compiler accepts it (makes me happy), but is it allowed by the standard
> to get a non-const reference from a mutable member inside of a const
> object? If it is allowed, where can I find it written down? So far I have
> found nothing :-(

It's _exactly_ what mutable is for.

--

Valentin Bonnard
---
[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "Gabor Greif" <gabor@no.netopia.com>
Date: 1999/09/30
Raw View
Consider the following snippet:

struct Ooops
{
 mutable int i;
 Ooops(void) : i(42) { }
};

int main(void)
{
 const Ooops o;

 int& ii(o.i); // hey it works! :-) -- but is it legal?
}


My compiler accepts it (makes me happy), but is it allowed by the standard
to get a non-const reference from a mutable member inside of a const
object? If it is allowed, where can I find it written down? So far I have
found nothing :-(

Thanks,

 Gabor




[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]