public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/30331]  New: a const member function can call a non_const member function without const_cast
@ 2006-12-30  7:58 hongleij at 126 dot com
  2006-12-30  8:09 ` [Bug c++/30331] " pinskia at gcc dot gnu dot org
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: hongleij at 126 dot com @ 2006-12-30  7:58 UTC (permalink / raw)
  To: gcc-bugs

//const_test.cpp
struct A
{
  A(unsigned int n)
  {
     aa=n;        
  }
  void const_fun() const
  {
      static_cast<A>(*this).fun();  //ok
     // fun();       //err  
  }
  void fun()
  {
  }
  unsigned int aa;
};

int main()
{
    const A a(56);
    a.const_fun();
}

//  g++ const_test.cpp
//  ./a.out
//we should forbid call non_const member function  in a const member function
without an explicit const_cast ,isn't it?


-- 
           Summary: a const member function can call a non_const member
                    function without const_cast
           Product: gcc
           Version: 4.1.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: hongleij at 126 dot com
  GCC host triplet: all


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=30331


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

* [Bug c++/30331] a const member function can call a non_const member function without const_cast
  2006-12-30  7:58 [Bug c++/30331] New: a const member function can call a non_const member function without const_cast hongleij at 126 dot com
@ 2006-12-30  8:09 ` pinskia at gcc dot gnu dot org
  2006-12-30 11:21 ` gdr at integrable-solutions dot net
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2006-12-30  8:09 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from pinskia at gcc dot gnu dot org  2006-12-30 08:08 -------
The following is ok:
      static_cast<A>(*this).fun();  //ok

Because you are creating a rvalue to hold "*this" which is then bound to a
const lvalue.

The following is an error:
      fun();       //err  
Because func is not a non_const.

Or I am missing something because all the versions of GCC I tried give an
error.


-- 

pinskia at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
      Known to work|                            |4.0.1 4.2.0 4.3.0


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=30331


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

* [Bug c++/30331] a const member function can call a non_const member function without const_cast
  2006-12-30  7:58 [Bug c++/30331] New: a const member function can call a non_const member function without const_cast hongleij at 126 dot com
  2006-12-30  8:09 ` [Bug c++/30331] " pinskia at gcc dot gnu dot org
@ 2006-12-30 11:21 ` gdr at integrable-solutions dot net
  2006-12-30 13:46 ` hongleij at 126 dot com
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: gdr at integrable-solutions dot net @ 2006-12-30 11:21 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from gdr at integrable-solutions dot net  2006-12-30 11:21 -------
Subject: Re:   New: a const member function can call a non_const member
function without const_cast

"hongleij at 126 dot com" <gcc-bugzilla@gcc.gnu.org> writes:

| //const_test.cpp
| struct A
| {
|   A(unsigned int n)
|   {
|      aa=n;        
|   }
|   void const_fun() const
|   {
|       static_cast<A>(*this).fun();  //ok

yes, as mandated by C++ semantics -- the static_cast creates a
new object to which fun() is applied.  That is OK.

|      // fun();       //err  

direct call to fun() is an error.

So, this PR does not report an error.  It should be closed as "mistaken".

-- Gaby


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=30331


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

* [Bug c++/30331] a const member function can call a non_const member function without const_cast
  2006-12-30  7:58 [Bug c++/30331] New: a const member function can call a non_const member function without const_cast hongleij at 126 dot com
  2006-12-30  8:09 ` [Bug c++/30331] " pinskia at gcc dot gnu dot org
  2006-12-30 11:21 ` gdr at integrable-solutions dot net
@ 2006-12-30 13:46 ` hongleij at 126 dot com
  2006-12-30 13:49 ` hongleij at 126 dot com
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: hongleij at 126 dot com @ 2006-12-30 13:46 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from hongleij at 126 dot com  2006-12-30 13:46 -------
the question is : if a const member function can call a non_const member
function
without const_cast,
if fun() changed the member in A,like this:
//const_test.cpp
struct A
{
  A(unsigned int n)
  {
     aa=n;        
  }
  void const_fun() const
  {
      static_cast<A>(*this).fun();  //ok
     // fun();       //err  
  }
  void fun()
  {
     aa=78;
  }
  unsigned int aa;
};

int main()
{
    const A a(56);
    a.const_fun();
}
the sematic of bitwise constness is not confirmed.
this program is inspired from the book <Effective C++>(third edition)
Item3: use const whenever possible


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=30331


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

* [Bug c++/30331] a const member function can call a non_const member function without const_cast
  2006-12-30  7:58 [Bug c++/30331] New: a const member function can call a non_const member function without const_cast hongleij at 126 dot com
                   ` (2 preceding siblings ...)
  2006-12-30 13:46 ` hongleij at 126 dot com
@ 2006-12-30 13:49 ` hongleij at 126 dot com
  2006-12-30 17:51 ` gdr at integrable-solutions dot net
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: hongleij at 126 dot com @ 2006-12-30 13:49 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #4 from hongleij at 126 dot com  2006-12-30 13:48 -------
(In reply to comment #3)
> the question is : if a const member function can call a non_const member
> function
> without const_cast,
> if fun() changed the member in A,like this:
> //const_test.cpp
> struct A
> {
>   A(unsigned int n)
>   {
>      aa=n;        
>   }
>   void const_fun() const
>   {
>       static_cast<A>(*this).fun();  //ok
>      // fun();       //err  
>   }
>   void fun()
>   {
>      aa=78;
>   }
>   unsigned int aa;
> };
> 
> int main()
> {
>     const A a(56);
>     a.const_fun();
> }
> the sematic of bitwise constness is not confirmed.
> this program is inspired from the book <Effective C++>(third edition)
> Item3: use const whenever possible
  i do hope the compiler at least give a worning!
> 


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=30331


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

* [Bug c++/30331] a const member function can call a non_const member function without const_cast
  2006-12-30  7:58 [Bug c++/30331] New: a const member function can call a non_const member function without const_cast hongleij at 126 dot com
                   ` (3 preceding siblings ...)
  2006-12-30 13:49 ` hongleij at 126 dot com
@ 2006-12-30 17:51 ` gdr at integrable-solutions dot net
  2006-12-30 17:53 ` gdr at gcc dot gnu dot org
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: gdr at integrable-solutions dot net @ 2006-12-30 17:51 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #5 from gdr at integrable-solutions dot net  2006-12-30 17:51 -------
Subject: Re:  a const member function can call a non_const member function
without const_cast

"hongleij at 126 dot com" <gcc-bugzilla@gcc.gnu.org> writes:

| the question is : if a const member function can call a non_const member
| function
| without const_cast,

[...]

| this program is inspired from the book <Effective C++>(third edition)
| Item3: use const whenever possible

This question is better asked and answered on C++ newsgroup.

-- Gaby


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=30331


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

* [Bug c++/30331] a const member function can call a non_const member function without const_cast
  2006-12-30  7:58 [Bug c++/30331] New: a const member function can call a non_const member function without const_cast hongleij at 126 dot com
                   ` (5 preceding siblings ...)
  2006-12-30 17:53 ` gdr at gcc dot gnu dot org
@ 2006-12-30 17:53 ` pinskia at gcc dot gnu dot org
  2006-12-31  2:24 ` hongleij at 126 dot com
  7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2006-12-30 17:53 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #7 from pinskia at gcc dot gnu dot org  2006-12-30 17:53 -------
Invalid as explained by me and Gaby.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=30331


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

* [Bug c++/30331] a const member function can call a non_const member function without const_cast
  2006-12-30  7:58 [Bug c++/30331] New: a const member function can call a non_const member function without const_cast hongleij at 126 dot com
                   ` (4 preceding siblings ...)
  2006-12-30 17:51 ` gdr at integrable-solutions dot net
@ 2006-12-30 17:53 ` gdr at gcc dot gnu dot org
  2006-12-30 17:53 ` pinskia at gcc dot gnu dot org
  2006-12-31  2:24 ` hongleij at 126 dot com
  7 siblings, 0 replies; 9+ messages in thread
From: gdr at gcc dot gnu dot org @ 2006-12-30 17:53 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #6 from gdr at gcc dot gnu dot org  2006-12-30 17:52 -------
user mistaken.


-- 

gdr at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=30331


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

* [Bug c++/30331] a const member function can call a non_const member function without const_cast
  2006-12-30  7:58 [Bug c++/30331] New: a const member function can call a non_const member function without const_cast hongleij at 126 dot com
                   ` (6 preceding siblings ...)
  2006-12-30 17:53 ` pinskia at gcc dot gnu dot org
@ 2006-12-31  2:24 ` hongleij at 126 dot com
  7 siblings, 0 replies; 9+ messages in thread
From: hongleij at 126 dot com @ 2006-12-31  2:24 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #8 from hongleij at 126 dot com  2006-12-31 02:24 -------
(In reply to comment #7)
> Invalid as explained by me and Gaby.
> 

yes,it's my misuse.
really sorry for my wrong report.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=30331


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

end of thread, other threads:[~2006-12-31  2:24 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-12-30  7:58 [Bug c++/30331] New: a const member function can call a non_const member function without const_cast hongleij at 126 dot com
2006-12-30  8:09 ` [Bug c++/30331] " pinskia at gcc dot gnu dot org
2006-12-30 11:21 ` gdr at integrable-solutions dot net
2006-12-30 13:46 ` hongleij at 126 dot com
2006-12-30 13:49 ` hongleij at 126 dot com
2006-12-30 17:51 ` gdr at integrable-solutions dot net
2006-12-30 17:53 ` gdr at gcc dot gnu dot org
2006-12-30 17:53 ` pinskia at gcc dot gnu dot org
2006-12-31  2:24 ` hongleij at 126 dot com

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