5 ms·
>>we'd have to get used to using auto_ptr >auto_ptr is broken. We should use shared_ptr instead It has begun! One of the most enjoyable features of C++ is th
by parfe 14y ago
>>we'd have to get used to using auto_ptr
>auto_ptr is broken. We should use shared_ptr instead
It has begun!
One of the most enjoyable features of C++ is the arguing over which parts of the language should be allowed.
- Paul_S 14y agoI avoid both but how is auto_ptr broken?
- milliams 14y agoLack of move semantics.
- Locke1689 14y agoauto_ptr's copy is broken, so if you copy one the first object loses the reference. Also, it always uses delete on the reference, which means it can't be used for things that don't use delete (like arrays which use delete[]).
- defap 14y agoBefore the days of std::move, auto_ptr's copy modeled an ownership-transfer semantic in a more efficient way than could be accomplished by shared_ptr (no refcount churn). It wasn't broken; one must have simply known when it was appropriate to use. Consider a helper function that returns a heap-allocated object and think whether auto_ptr or shared_ptr better modeled the function's intent of giving ownership of the object to the caller.
- shrughes 14y agoThe problem with auto_ptr is that it put these ownership-transfer semantics in the copy constructor instead of something more explicit. A scoped_ptr type that has a release() method is far more useful and safer than auto_ptr.
- defap 14y agoSure. I was more responding to the claim that shared_ptr should always be preferred to auto_ptr. I agree that scoped/unique_ptr fixed a lot if auto_ptr's pitfalls.
- obiterdictum 14y agoThis was just a workaround for the lack of move constructors. The real problem with auto_ptr is that if anything emits a call to auto_ptr destructor at the point where the object pointed to is incomplete (forward-declared), the destructor code won't run, only memory would be freed.
- Moto7451 14y agoIn addition to it being broken, in C++11 auto_ptr is depreciated. Since they're moving to C++ now, they may as well use the latest features as often as possible (when the use case makes sense of course).
- deleted 14y ago[deleted]
- klodolph 14y agoNo, it's a bad idea to use the latest features. GCC still has to be able to bootstrap. This is why it was still using C90 in the first place.
- Negitivefrags 14y agoJust to be clear, the replacement for auto_ptr is unique_ptr, not shared_ptr. The vast majority of allocated objects have unique ownership semantics, not shared. If you use shared_ptr everywhere, it's far too easy for your design to degenerate into object soup.
- dmm 14y agoIf the ownership semantics are clear, scoped_ptr works too.
- TillE 14y ago> If you use shared_ptr everywhere, it's far too easy for your design to degenerate into object soup. You mean that it's usually better design to have only one "owner" of an object? That's true. It's worth noting that shared_ptr is also necessary for containers, so you might get into a habit of using it even if you're not creating multiple references.
- Negitivefrags 14y agoNo it isn't. unique_ptr works very well in containers.
- eliben 14y agoNot only. Over-use of shared_ptr is sloppy, and may cause reference leaks which are no easier to solve than memory leaks. Its use is recommended only when sharing semantics are actually required.
- shrughes 14y agoshared_ptr isn't necessary for containers, it's only necessary for bad containers. Edit: Of course I'm being a bit stupidly snarky here, obviously if you have a standard container you need a shared_ptr, or an intrusive_ptr, or some other pointer, the moral of the story is that std containers aren't good for holding owned pointers in C++03. I don't think boost ptr_containers are particularly good either, but that's mostly because of the huge cost of putting the string '#include "boost/' in your code.
- deleted 14y ago
- malkia 14y agoAnd everybody knows that ++C is often more efficient than C++
- astrodust 14y agoThat would be an in-place update to C. It's happened a few times, it's now C11 instead of C99. Sort-ability went out the window because of Y2K.