[Bug c++/34180] New: Default copy constructor copies const auto_ptr members
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_ptrint 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
[Bug c++/34180] Default copy constructor copies const auto_ptr members
--- 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_ptrint a; }; A a; A b = a; } -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=34180
[Bug c++/34180] Default copy constructor copies const auto_ptr members
--- 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_ptrint i; const auto_ptrint 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
[Bug c++/34180] Default copy constructor copies const auto_ptr members
--- 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