Skip to main content
12 events
when toggle format what by license comment
Aug 16, 2017 at 8:46 vote accept Gergely Tomcsányi
Aug 15, 2017 at 22:48 answer added Loki Astari timeline score: 3
Aug 15, 2017 at 22:35 comment added Loki Astari I am sure there are other issues. But they are so far away and long ago that I have forgotten. A good rule of thumb is don't put auto_ptr in a vector. Use a boost::ptr_vector instead.
Aug 15, 2017 at 22:33 comment added Loki Astari The main restriction on vectors is that objects in them must be copyable (which also means they don't change when copied). If you don't break this constraint (ie you don't copy the content) when using auto_ptr then it will work fine. But if you accidentally cause a copy then you have broken the constraint of the vector and thus its guarantees. Unfortunately this can happen without generating a compiler error or warning. Thus using auto_ptr in a vector is dangerous.
Aug 15, 2017 at 22:32 comment added Loki Astari You can use auto_ptr in vector but it has severe limitations that are really easy to violate. The real problem is that violating these constraints did not cause a compiler error and resulted in some very strange and unexpected consequence.
Aug 15, 2017 at 10:49 comment added Gergely Tomcsányi @TobySpeight - I just don't understand, why they couldn't add a better auto_ptr to the standard library before move semantics and rvalue reference were invented. If I was able to create a better one with C++03 tools... Maybe it's an ugly solution, but it would have been better than the almost useless auto_ptr before C++11.
Aug 15, 2017 at 10:41 comment added Gergely Tomcsányi @DDrmmr - Can you explain it a bit clearer, why I can't still use it with std::vector? In fact, I was able to push_back owner_ptr objects into the container. Also, what is considered to be third party code? I'm using only the C++03 standard library. I've created this template for personal use only.
Aug 15, 2017 at 8:01 comment added Toby Speight To answer your final question - why, up to C++11, the only smart pointer was std::auto_ptr: because it's a hard problem, and required the invention of move semantics, which in turn caused the invention of rvalue references and substantial changes to the language to support this part of the Standard Library. It's not the kind of thing that can be quietly added as an afterthought.
Aug 15, 2017 at 0:14 comment added D Drmmr Sounds like you are trying to reimplement std::unique_ptr without move semantics. That's impossible. std::auto_ptr can't be used with std::vector, because the copy c'tor passes ownership. std::unique_ptr fixes this by not being copyable, but only movable. Now, you can emulate move semantics in C++03, but third party code won't be able to use your emulation. So, you still can't use your class together with std::vector.
Aug 14, 2017 at 23:54 history edited Gergely Tomcsányi CC BY-SA 3.0
added 3 characters in body
Aug 14, 2017 at 23:47 history edited Gergely Tomcsányi CC BY-SA 3.0
added 14 characters in body
Aug 14, 2017 at 23:41 history asked Gergely Tomcsányi CC BY-SA 3.0