public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/34180]  New: Default copy constructor copies const auto_ptr members
@ 2007-11-21 22:42 jeidsath at gmail dot com
  2007-11-21 22:45 ` [Bug c++/34180] " jeidsath at gmail dot com
                   ` (11 more replies)
  0 siblings, 12 replies; 13+ messages in thread
From: jeidsath at gmail dot com @ 2007-11-21 22:42 UTC (permalink / raw)
  To: gcc-bugs

It is an error to assign or copy a const auto_ptr. g++ should refuse to compile
the following code:

"bug.cpp":
#include <memory>
int main()
{
        class A
        {
                const std::auto_ptr<int> a;
        };
        A a;
}

Compiled with:
g++ bug.cpp

This code compiles, incorrectly, on the following version of gcc:

Using built-in specs.
Target: i386-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man
--infodir=/usr/share/info --enable-shared --enable-threads=posix
--enable-checking=release --with-system-zlib --enable-__cxa_atexit
--disable-libunwind-exceptions
--enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk
--disable-dssi --enable-plugin
--with-java-home=/usr/lib/jvm/java-1.5.0-gcj-1.5.0.0/jre
--enable-libgcj-multifile --enable-java-maintainer-mode
--with-ecj-jar=/usr/share/java/eclipse-ecj.jar --with-cpu=generic
--host=i386-redhat-linux
Thread model: posix
gcc version 4.1.2 20070502 (Red Hat 4.1.2-12)


-- 
           Summary: Default copy constructor copies const auto_ptr members
           Product: gcc
           Version: 4.1.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: jeidsath at gmail dot com


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


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

* [Bug c++/34180] Default copy constructor copies const auto_ptr members
  2007-11-21 22:42 [Bug c++/34180] New: Default copy constructor copies const auto_ptr members jeidsath at gmail dot com
@ 2007-11-21 22:45 ` jeidsath at gmail dot com
  2007-11-21 23:21 ` pcarlini at suse dot de
                   ` (10 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: jeidsath at gmail dot com @ 2007-11-21 22:45 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from jeidsath at gmail dot com  2007-11-21 22:45 -------
I apologize, a line was incorrectly cut from the copy and paste, here is the
full code that should not compile, but does:

#include <memory>
int main()
{
        class A
        {
                const std::auto_ptr<int> a;
        };
        A a;
        A b = a;
}


-- 


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


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

* [Bug c++/34180] Default copy constructor copies const auto_ptr members
  2007-11-21 22:42 [Bug c++/34180] New: Default copy constructor copies const auto_ptr members jeidsath at gmail dot com
  2007-11-21 22:45 ` [Bug c++/34180] " jeidsath at gmail dot com
@ 2007-11-21 23:21 ` pcarlini at suse dot de
  2007-11-21 23:52 ` jeidsath at gmail dot com
                   ` (9 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: pcarlini at suse dot de @ 2007-11-21 23:21 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from pcarlini at suse dot de  2007-11-21 23:21 -------
I think this is the issue, nothing specific to std::auto_ptr:

struct G { G() { } G(G&) { } };

int main()
{
  class A
  {
    const G g;
  };
  A a;
  A b = a;
}


-- 


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


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

* [Bug c++/34180] Default copy constructor copies const auto_ptr members
  2007-11-21 22:42 [Bug c++/34180] New: Default copy constructor copies const auto_ptr members jeidsath at gmail dot com
  2007-11-21 22:45 ` [Bug c++/34180] " jeidsath at gmail dot com
  2007-11-21 23:21 ` pcarlini at suse dot de
@ 2007-11-21 23:52 ` jeidsath at gmail dot com
  2007-11-22  0:10 ` pcarlini at suse dot de
                   ` (8 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: jeidsath at gmail dot com @ 2007-11-21 23:52 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from jeidsath at gmail dot com  2007-11-21 23:52 -------
(In reply to comment #2)
> I think this is the issue, nothing specific to std::auto_ptr:
> 
> struct G { G() { } G(G&) { } };
> 
> int main()
> {
>   class A
>   {
>     const G g;
>   };
>   A a;
>   A b = a;
> }
> 

No, copying of const values is certainly allowed:

const int i = 5;
const int i2 = i; //SHOULD compile

Your struct G can also be created and copied:

G g;
G g2 = g;

However, the same does not work with const auto_ptr, because copying
is a non-const operation for auto_ptrs:

const auto_ptr<int> i;
const auto_ptr<int> i2 = i; //ERROR

Did you mean to make the copy constructor private? In that case, G
certainly won't work in my example, const or not, and g++ catches
this.

Now, if you're trying to make a more general example, try this:

#include <iostream>
struct H
{
       H()
       { a=5; } ;
       H(H& rhs)
       { rhs.a=10; };
       int a;
};

int main()
{
       struct B
       {
               const H h;
       };
       B b;
       B b2 = b;
       std::cout << b.h.a << "\n";//ERROR prints out "10",
       //b.h has been changed despite constness
}


The copy constructor for H does not take a const reference because I
am modelling auto_ptr -- compare gcc's definition of the auto_ptr copy
constructor in "memory":
     auto_ptr(auto_ptr& __a) throw() : _M_ptr(__a.release()) { }

Joel Eidsath


-- 


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


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

* [Bug c++/34180] Default copy constructor copies const auto_ptr members
  2007-11-21 22:42 [Bug c++/34180] New: Default copy constructor copies const auto_ptr members jeidsath at gmail dot com
                   ` (2 preceding siblings ...)
  2007-11-21 23:52 ` jeidsath at gmail dot com
@ 2007-11-22  0:10 ` pcarlini at suse dot de
  2007-11-22  0:24 ` jeidsath at gmail dot com
                   ` (7 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: pcarlini at suse dot de @ 2007-11-22  0:10 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #4 from pcarlini at suse dot de  2007-11-22 00:10 -------
(In reply to comment #3)
> (In reply to comment #2)
> > I think this is the issue, nothing specific to std::auto_ptr:
> > 
> > struct G { G() { } G(G&) { } };
> > 
> > int main()
> > {
> >   class A
> >   {
> >     const G g;
> >   };
> >   A a;
> >   A b = a;
> > }
> > 
> 
> No, copying of const values is certainly allowed:
> 
> const int i = 5;
> const int i2 = i; //SHOULD compile
> 
> Your struct G can also be created and copied:
> 
> G g;
> G g2 = g;

Of course, and of course. But that has nothing to do with my reduced snippet,
which is equivalent to our standard-conforming implementation of std::auto_ptr,
as far as I can see, and, does compile, whereas it should not - to be clear, I
think you are therefore right, just there is nothing wrong with our
implementation of std::auto_ptr, if anything, this is a front-end issue. By the
way, ICC rejects my reduced snippet.


-- 


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


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

* [Bug c++/34180] Default copy constructor copies const auto_ptr members
  2007-11-21 22:42 [Bug c++/34180] New: Default copy constructor copies const auto_ptr members jeidsath at gmail dot com
                   ` (3 preceding siblings ...)
  2007-11-22  0:10 ` pcarlini at suse dot de
@ 2007-11-22  0:24 ` jeidsath at gmail dot com
  2007-11-25  2:59 ` pinskia at gcc dot gnu dot org
                   ` (6 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: jeidsath at gmail dot com @ 2007-11-22  0:24 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #5 from jeidsath at gmail dot com  2007-11-22 00:24 -------
> Of course, and of course. But that has nothing to do with my reduced snippet,
> which is equivalent to our standard-conforming implementation of std::auto_ptr,
> as far as I can see, and, does compile, whereas it should not - to be clear, I
> think you are therefore right, just there is nothing wrong with our
> implementation of std::auto_ptr, if anything, this is a front-end issue. By the
> way, ICC rejects my reduced snippet.

My fault, I see now -- I should have noticed that your copy constructor was a
non-const function. I agree that this is a front-end issue.


-- 

jeidsath at gmail dot com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jeidsath at gmail dot com


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


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

* [Bug c++/34180] Default copy constructor copies const auto_ptr members
  2007-11-21 22:42 [Bug c++/34180] New: Default copy constructor copies const auto_ptr members jeidsath at gmail dot com
                   ` (4 preceding siblings ...)
  2007-11-22  0:24 ` jeidsath at gmail dot com
@ 2007-11-25  2:59 ` pinskia at gcc dot gnu dot org
  2009-11-05 21:16 ` likeuclinux at yahoo dot ca
                   ` (5 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2007-11-25  2:59 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #6 from pinskia at gcc dot gnu dot org  2007-11-25 02:59 -------
With types checking on the trunk, I actually get an ICE:
t.cc:8: error: type mismatch in address expression
struct G *

const struct G *

D.2051 = &D.2032->g
t.cc:8: internal compiler error: verify_gimple failed
Please submit a full bug report,
with preprocessed source if appropriate.
See <http://gcc.gnu.org/bugs.html> for instructions.


Confirmed.
This used to work in 3.4.0 and before:
t.cc: In function `int main()':
t.cc:9: error: no matching function for call to `main()::A::A()'
t.cc:6: note: candidates are: main()::A::A(main()::A&)


I don't think I could count this regression.  GCC in 3.4.0 and before was not
producing the normal constructor.


-- 

pinskia at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
     Ever Confirmed|0                           |1
           Keywords|                            |accepts-invalid, ice-on-
                   |                            |valid-code
      Known to fail|                            |4.1.1 4.1.2 4.2.0 4.3.0
   Last reconfirmed|0000-00-00 00:00:00         |2007-11-25 02:59:13
               date|                            |


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


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

* [Bug c++/34180] Default copy constructor copies const auto_ptr members
  2007-11-21 22:42 [Bug c++/34180] New: Default copy constructor copies const auto_ptr members jeidsath at gmail dot com
                   ` (5 preceding siblings ...)
  2007-11-25  2:59 ` pinskia at gcc dot gnu dot org
@ 2009-11-05 21:16 ` likeuclinux at yahoo dot ca
  2009-11-05 22:11 ` paolo dot carlini at oracle dot com
                   ` (4 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: likeuclinux at yahoo dot ca @ 2009-11-05 21:16 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #7 from likeuclinux at yahoo dot ca  2009-11-05 21:16 -------
Please, this is definitely bug from GCC, for same code give me error in Visual
Studio 2008 but not in GCC:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <memory>
using namespace std;
class A{
public:
  A() : p(new int(10)){
  }
  const auto_ptr<int> p;
};
int main() {
  A a;
  printf("a pointer=0x%x\n", a.p.get());
  A a2 = a;
  printf("a pointer=0x%x\n", a.p.get());
  printf("a2 pointer=0x%x\n", a2.p.get());
  return 0;
}

Error:

Error   1       error C2558: class 'std::auto_ptr<_Ty>' : no copy constructor
available or copy constructor is declared 'explicit'      c:\documents and
settings\charlie\my documents\visual studio 2008\projects\debug\debug\test.cpp
19      debug


-- 


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


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

* [Bug c++/34180] Default copy constructor copies const auto_ptr members
  2007-11-21 22:42 [Bug c++/34180] New: Default copy constructor copies const auto_ptr members jeidsath at gmail dot com
                   ` (6 preceding siblings ...)
  2009-11-05 21:16 ` likeuclinux at yahoo dot ca
@ 2009-11-05 22:11 ` paolo dot carlini at oracle dot com
  2009-11-06  4:03 ` jason at gcc dot gnu dot org
                   ` (3 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: paolo dot carlini at oracle dot com @ 2009-11-05 22:11 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #8 from paolo dot carlini at oracle dot com  2009-11-05 22:11 -------
Jason, this isn't a regression, but a very old issue, any chance you can have a
look? As far as I can see we are still compiling the snipped in Comment #2 and
the EDG-based compilers are still rejecting it...


-- 

paolo dot carlini at oracle dot com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jason at gcc dot gnu dot org


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


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

* [Bug c++/34180] Default copy constructor copies const auto_ptr members
  2007-11-21 22:42 [Bug c++/34180] New: Default copy constructor copies const auto_ptr members jeidsath at gmail dot com
                   ` (7 preceding siblings ...)
  2009-11-05 22:11 ` paolo dot carlini at oracle dot com
@ 2009-11-06  4:03 ` jason at gcc dot gnu dot org
  2009-11-06  4:32 ` jason at gcc dot gnu dot org
                   ` (2 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: jason at gcc dot gnu dot org @ 2009-11-06  4:03 UTC (permalink / raw)
  To: gcc-bugs



-- 

jason at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         AssignedTo|unassigned at gcc dot gnu   |jason at gcc dot gnu dot org
                   |dot org                     |
             Status|NEW                         |ASSIGNED
   Last reconfirmed|2008-03-30 20:47:06         |2009-11-06 04:02:46
               date|                            |


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


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

* [Bug c++/34180] Default copy constructor copies const auto_ptr members
  2007-11-21 22:42 [Bug c++/34180] New: Default copy constructor copies const auto_ptr members jeidsath at gmail dot com
                   ` (8 preceding siblings ...)
  2009-11-06  4:03 ` jason at gcc dot gnu dot org
@ 2009-11-06  4:32 ` jason at gcc dot gnu dot org
  2009-11-06 10:35 ` paolo dot carlini at oracle dot com
  2010-02-19 21:41 ` jason at gcc dot gnu dot org
  11 siblings, 0 replies; 13+ messages in thread
From: jason at gcc dot gnu dot org @ 2009-11-06  4:32 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #9 from jason at gcc dot gnu dot org  2009-11-06 04:32 -------
Subject: Bug 34180

Author: jason
Date: Fri Nov  6 04:32:13 2009
New Revision: 153960

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=153960
Log:
        PR c++/34180
        * method.c (do_build_copy_constructor): Don't drop cv-quals from
        the field type.

Added:
    trunk/gcc/testsuite/g++.dg/init/synth2.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/method.c
    trunk/gcc/testsuite/ChangeLog


-- 


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


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

* [Bug c++/34180] Default copy constructor copies const auto_ptr members
  2007-11-21 22:42 [Bug c++/34180] New: Default copy constructor copies const auto_ptr members jeidsath at gmail dot com
                   ` (9 preceding siblings ...)
  2009-11-06  4:32 ` jason at gcc dot gnu dot org
@ 2009-11-06 10:35 ` paolo dot carlini at oracle dot com
  2010-02-19 21:41 ` jason at gcc dot gnu dot org
  11 siblings, 0 replies; 13+ messages in thread
From: paolo dot carlini at oracle dot com @ 2009-11-06 10:35 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #10 from paolo dot carlini at oracle dot com  2009-11-06 10:34 -------
Thanks.


-- 


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


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

* [Bug c++/34180] Default copy constructor copies const auto_ptr members
  2007-11-21 22:42 [Bug c++/34180] New: Default copy constructor copies const auto_ptr members jeidsath at gmail dot com
                   ` (10 preceding siblings ...)
  2009-11-06 10:35 ` paolo dot carlini at oracle dot com
@ 2010-02-19 21:41 ` jason at gcc dot gnu dot org
  11 siblings, 0 replies; 13+ messages in thread
From: jason at gcc dot gnu dot org @ 2010-02-19 21:41 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #11 from jason at gcc dot gnu dot org  2010-02-19 21:41 -------
Fixed.


-- 

jason at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|                            |FIXED
   Target Milestone|---                         |4.5.0


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


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

end of thread, other threads:[~2010-02-19 21:41 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-11-21 22:42 [Bug c++/34180] New: Default copy constructor copies const auto_ptr members jeidsath at gmail dot com
2007-11-21 22:45 ` [Bug c++/34180] " jeidsath at gmail dot com
2007-11-21 23:21 ` pcarlini at suse dot de
2007-11-21 23:52 ` jeidsath at gmail dot com
2007-11-22  0:10 ` pcarlini at suse dot de
2007-11-22  0:24 ` jeidsath at gmail dot com
2007-11-25  2:59 ` pinskia at gcc dot gnu dot org
2009-11-05 21:16 ` likeuclinux at yahoo dot ca
2009-11-05 22:11 ` paolo dot carlini at oracle dot com
2009-11-06  4:03 ` jason at gcc dot gnu dot org
2009-11-06  4:32 ` jason at gcc dot gnu dot org
2009-11-06 10:35 ` paolo dot carlini at oracle dot com
2010-02-19 21:41 ` jason at gcc dot gnu dot org

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