auto-ptr

std::auto_ptr to std::unique_ptr

会有一股神秘感。 提交于 2019-11-26 21:17:43
With the new standard coming (and parts already available in some compilers), the new type std::unique_ptr is supposed to be a replacement for std::auto_ptr . Does their usage exactly overlap (so I can do a global find/replace on my code (not that I would do this, but if I did)) or should I be aware of some differences that are not apparent from reading the documentation? Also if it is a direct replacement, why give it a new name rather than just improve the std::auto_ptr ? Cubbi You cannot do a global find/replace because you can copy an auto_ptr (with known consequences), but a unique_ptr

Smart pointers in container like std::vector?

为君一笑 提交于 2019-11-26 20:13:06
问题 I am learning about smart pointers ( std::auto_ptr ) and just read here and here that smart pointers ( std::auto_ptr ) should not be put in containers (i.e. std::vector ) because even most compilers won't complain and it might seem correct. There is no rule that says smart pointers won't be copied internally (by vector class for example) and transfer its ownership, then the pointer will become NULL. In the end, everything will be screwed up. In reality, how often does this happen? Sometimes I

How could one implement std::auto_ptr's copy constructor?

旧街凉风 提交于 2019-11-26 16:55:59
问题 Back on my crazy AutoArray thingy... (quoting important bits from there: class AutoArray { void * buffer; public: //Creates a new empty AutoArray AutoArray(); //std::auto_ptr copy semantics AutoArray(AutoArray&); //Note it can't be const because the "other" reference //is null'd on copy... AutoArray& operator=(AutoArray); ~AutoArray(); //Nothrow swap // Note: At the moment this method is not thread safe. void Swap(AutoArray&); }; ) Anyway, trying to implement the copy constructor. There's a

So can unique_ptr be used safely in stl collections?

筅森魡賤 提交于 2019-11-26 12:39:24
问题 I am confused with unique_ptr and rvalue move philosophy. Let\'s say we have two collections: std::vector<std::auto_ptr<int>> autoCollection; std::vector<std::unique_ptr<int>> uniqueCollection; Now I would expect the following to fail, as there is no telling what the algorithm is doing internally and maybe making internal pivot copies and the like, thus ripping away ownership from the auto_ptr: std::sort(autoCollection.begin(), autoCollection.end()); I get this. And the compiler rightly

Is auto_ptr deprecated?

ε祈祈猫儿з 提交于 2019-11-26 12:08:46
问题 Will auto_ptr be deprecated in incoming C++ standard? Should unique_ptr be used for ownership transfer instead of shared_ptr? If unique_ptr is not in the standard, then do I need to use shared_ptr instead? 回答1: UPDATE: This answer was written in 2010 and as anticipated std::auto_ptr has been deprecated. The advice is entirely valid. In C++0x std::auto_ptr will be deprecated in favor of std::unique_ptr . The choice of smart pointer will depend on your use case and your requirements, with std:

Why is auto_ptr being deprecated?

ε祈祈猫儿з 提交于 2019-11-26 11:55:02
问题 I heard auto_ptr is being deprecated in C++11. What is the reason for this? Also I would like to know the difference between auto_ptr and shared_ptr . 回答1: The direct replacement for auto_ptr (or the closest thing to one anyway) is unique_ptr. As far as the "problem" goes, it's pretty simple: auto_ptr transfers ownership when it's assigned. unique_ptr also transfers ownership, but thanks to codification of move semantics and the magic of rvalue references, it can do so considerably more

std::auto_ptr to std::unique_ptr

非 Y 不嫁゛ 提交于 2019-11-26 09:05:13
问题 With the new standard coming (and parts already available in some compilers), the new type std::unique_ptr is supposed to be a replacement for std::auto_ptr . Does their usage exactly overlap (so I can do a global find/replace on my code (not that I would do this, but if I did)) or should I be aware of some differences that are not apparent from reading the documentation? Also if it is a direct replacement, why give it a new name rather than just improve the std::auto_ptr ? 回答1: You cannot do

Why is it wrong to use std::auto_ptr<> with standard containers?

夙愿已清 提交于 2019-11-25 23:33:43
问题 Why is it wrong to use std::auto_ptr<> with standard containers? 回答1: The C++ Standard says that an STL element must be "copy-constructible" and "assignable." In other words, an element must be able to be assigned or copied and the two elements are logically independent. std::auto_ptr does not fulfill this requirement. Take for example this code: class X { }; std::vector<std::auto_ptr<X> > vecX; vecX.push_back(new X); std::auto_ptr<X> pX = vecX[0]; // vecX[0] is assigned NULL. To overcome