Questions tagged [auto-ptr]

A C++ template class that provides a limited garbage collection facility for pointers, by allowing pointers to have the elements they point to automatically destroyed when the auto_ptr object is itself destroyed. Deprecated as of C++11 in favor of unique_ptr.

auto_ptr is deprecated as of C++11. For the C++11 equivalent, unique_ptr, see the reference page.

Reference page for auto_ptr.

197 questions
5
votes
2 answers

Singleton pattern: different behavior of auto_ptr and unique_ptr

While implementing a factory class I encountered a behavior of std::auto_ptr that I am not able to understand. I reduced the problem down to the following small program, so ... let's start. Consider the following singleton class: singleton.h #ifndef…
Massimiliano
  • 7,842
  • 2
  • 47
  • 62
5
votes
2 answers

return value optimization vs auto_ptr for large vectors

If I use auto_ptr as a return value of a function that populates large vectors, this makes the function a source function (it will create an internal auto_ptr and pass over ownership when it returns a non const auto_ptr). However, I cannot use this…
tmaric
  • 5,347
  • 4
  • 42
  • 75
5
votes
5 answers

Returning a new object along with another value

I want to return two values, one of which is a new object. I can do this using std::pair: class A { //... }; std::pair getA() { A* a = new A; //... } To make the code exception-safe, I would like to do: std::pair,…
amit kumar
  • 20,438
  • 23
  • 90
  • 126
4
votes
2 answers

How do I declare a dynamic array with std::auto_ptr?

I am trying to declare a dynamic int array like below: int n; int *pInt = new int[n]; Can I do this with std::auto_ptr? I tried something like: std::auto_ptr pInt(new int[n]); But it doesn't compile. I'm wondering if I could declare a…
itnovice
  • 503
  • 1
  • 4
  • 20
4
votes
3 answers

Will auto_ptr protect against this?

I am not quite clear if auto_ptr will help me in this case: class A { A(const B& member) : _member(B) {}; ... const B& _member; }; A generateA() { auto_ptr smart(new B()); A myA(*smart); return myA; } Will the myA._member…
Max Maximus
  • 779
  • 10
  • 14
4
votes
3 answers

C++ -- Is there an implicit cast here from Fred* to auto_ptr?

I saw the following code, #include #include using namespace std; class Fred; // Forward declaration typedef auto_ptr FredPtr; class Fred { public: static FredPtr create(int i) { return new Fred(i); // Is there an…
q0987
  • 34,938
  • 69
  • 242
  • 387
4
votes
1 answer

why there are template copy constructor and override operator function in auto_ptr?

why there are template copy constructor and override operator function in auto_ptr? The ISO standard for C++ specifies the following interface for auto_ptr. (This is copied straight out of the 2003 standard.) namespace std { template
BlackMamba
  • 10,054
  • 7
  • 44
  • 67
4
votes
2 answers

Can I get away with putting auto_ptr in a STL container?

I am inheriting an interface, and implementing a virtual function that is supposed to do some work on a list of dynamically allocated objects. The first step is to remove duplicates from the list based on some custom equivalence criteria: class Foo…
neuviemeporte
  • 6,310
  • 10
  • 49
  • 78
4
votes
1 answer

Advantages of unique_ptr over auto_ptr?

I do not fully understand the benefits of unique_ptr over auto_ptr, or I am not yet fully convinced why we need to use unique_ptr. I see the following differences. 1) unique_ptr supports arrays and thus the unique_ptr destructor calls delete [] for…
kadina
  • 5,042
  • 4
  • 42
  • 83
4
votes
1 answer

Trying to understand auto_ptr

I'm trying to understand certain details about how auto_ptr class works. Suppose you have the following class (i found this on a web site where the person explains the finer points of the assignment operator). class TFoo : public TSuperFoo { …
driftwood
  • 2,051
  • 4
  • 21
  • 28
4
votes
3 answers

Can I create an array of auto_ptr?

I have a base class, which is inherited by multiple derived classes. i want to create array of autopointer of baseClass pointer. when i initialise those autopointer i get some compile time error, then i tried to do like that…
user1808932
  • 427
  • 1
  • 5
  • 14
4
votes
4 answers

Is this a fine std::auto_ptr<> use case?

Please suppose I have a function that accepts a pointer as a parameter. This function can throw an exception, as it uses std::vector<>::push_back() to manage the lifecycle of this pointer. If I declare it like this: void manage(T *ptr); and call it…
Gui Prá
  • 5,559
  • 4
  • 34
  • 59
4
votes
3 answers

auto_ptr pointing to a dynamic array

In my code, I am allocating an integer array using new. After that I am wrapping this pointer to an auto_ptr. I know that the auto_ptr call its destructor automatically. Since my auto_ptr is pointing to an array (allocated using new), Will the array…
Aneesh Narayanan
  • 3,220
  • 11
  • 31
  • 48
4
votes
4 answers

Create a new object from existing pointer C++

I've looked for the answer but still can't figure this out. Sorry, but my work is too complex to copy here sample code. I have a function, which gets a pointer as parameter; I use it, but later, I need a kind of callback, where I want to use my old…
Steve M. Bay
  • 313
  • 1
  • 3
  • 15
4
votes
3 answers

converting a auto_ptr to a shared_ptr

How can I change an std::auto_ptr to a boost::shared_ptr? Here are my restrictions: 1. I am using an API class, lets call it only_auto that returns these pointers 2. I need to use the call in auto_only 3. My semantics involves sharing so I do…
user993833
  • 81
  • 2
  • 5