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