public inbox for gcc-prs@sourceware.org
help / color / mirror / Atom feed
* Re: c++/3760: g++ rejects legal code
@ 2001-07-21 12:26 Julian v. Bock
  0 siblings, 0 replies; 3+ messages in thread
From: Julian v. Bock @ 2001-07-21 12:26 UTC (permalink / raw)
  To: nobody; +Cc: gcc-prs

The following reply was made to PR c++/3760; it has been noted by GNATS.

From: julian@OpenIT.DE (Julian v. Bock)
To: "Uwe F. Mayer" <mayer@tux.org>
Cc: gcc-gnats@gcc.gnu.org
Subject: Re: c++/3760: g++ rejects legal code
Date: 21 Jul 2001 21:22:42 +0200

 Hi
 
 >>>>> "Uwe" =3D=3D Uwe F Mayer <mayer@tux.org> writes:
 
     Uwe> c++ rejects to compile a member function changing a mutable
     Uwe> data member of a constant object instance. Direct changes of
     Uwe> the mutable member (if public) are fine.
     >> How-To-Repeat:
     Uwe> Compile the program bug8.cc included below, and you will get
     Uwe> the compile-time error message: $ g++ bug8.cc bug8.cc: In
     Uwe> function `int main()': bug8.cc:19: passing `const class_m' as
     Uwe> `this' argument of `void class_m::incr_i()' discards
     Uwe> qualifiers
 
 This is not a compiler bug.
 The compiler complains because you are trying to call a non-const
 member function of a const object. ("passing `const class_m' as `this'
 argument of `void class_m::incr_i()' discards qualifiers").
 
 Make class_m::incr_i a const member function and you are fine.
 
 Julian
 
 --=20
 OpenIT GmbH                        Tel +49 211 239 577-0
 Julian v. Bock                     Fax +49 211 239 577-10
 Jahnstr. 18                        julian@openit.de
 40215 D=FCsseldorf                   http://www.openit.de


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: c++/3760: g++ rejects legal code
@ 2001-07-21 13:02 nathan
  0 siblings, 0 replies; 3+ messages in thread
From: nathan @ 2001-07-21 13:02 UTC (permalink / raw)
  To: gcc-bugs, gcc-prs, mayer, nobody

Synopsis: g++ rejects legal code

State-Changed-From-To: open->closed
State-Changed-By: nathan
State-Changed-When: Sat Jul 21 13:02:12 2001
State-Changed-Why:
    Not a bug. you misunderstand mutable. 

http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view&pr=3760&database=gcc


^ permalink raw reply	[flat|nested] 3+ messages in thread

* c++/3760: g++ rejects legal code
@ 2001-07-21 11:46 Uwe F. Mayer
  0 siblings, 0 replies; 3+ messages in thread
From: Uwe F. Mayer @ 2001-07-21 11:46 UTC (permalink / raw)
  To: gcc-gnats

>Number:         3760
>Category:       c++
>Synopsis:       g++ rejects legal code
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          rejects-legal
>Submitter-Id:   net
>Arrival-Date:   Sat Jul 21 11:46:00 PDT 2001
>Closed-Date:
>Last-Modified:
>Originator:     Uwe F. Mayer
>Release:        3.0
>Organization:
tux.org
>Environment:
System: Linux tosca 2.2.18 #2 Thu Mar 8 13:48:25 PST 2001 i686 unknown
Architecture: i686
host: i686-pc-linux-gnu
build: i686-pc-linux-gnu
target: i686-pc-linux-gnu
configured with: ../gcc-3.0/configure --disable-nls --prefix=/usr/local/gcc-3.0
>Description:
c++ rejects to compile a member function changing a mutable data member of
a constant object instance. Direct changes of the mutable member (if public)
are fine.
>How-To-Repeat:
Compile the program bug8.cc included below, and you will get the compile-time
error message:
$ g++ bug8.cc 
bug8.cc: In function `int main()':
bug8.cc:19: passing `const class_m' as `this' argument of `void 
   class_m::incr_i()' discards qualifiers

The program bug8.cc:
====================
#include <iostream>
using namespace std;

class class_m {
public:
  class_m() : i(0) {}
  void incr_i() {i++;}
  void print() const {cout << i << endl;}
  mutable int i;
};

int main() {
  const class_m obj_m;
  obj_m.print();
  obj_m.i++;                                // works
  obj_m.print();
  const_cast<class_m*>(&obj_m)->incr_i();   // works
  obj_m.print();
  obj_m.incr_i();                           // rejected at compile time
  obj_m.print();
}

>Fix:
Work-around: if function() is a mutator member function changing a mutable data
member of a constant object instance, use instead of:
obj_m.function()
the expression:
const_cast<class_m*>(&obj_m)->function()
>Release-Note:
>Audit-Trail:
>Unformatted:


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2001-07-21 13:02 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-07-21 12:26 c++/3760: g++ rejects legal code Julian v. Bock
  -- strict thread matches above, loose matches on Subject: below --
2001-07-21 13:02 nathan
2001-07-21 11:46 Uwe F. Mayer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).