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
3
votes
1 answer

issues on auto_ptr

suppose that,we have following code auto_ptr source() { return auto_ptr( new T(1) ); } void sink( auto_ptr pt ) { } void f() { auto_ptr a( source() ); sink( source() ); sink( auto_ptr( new T(1) ) ); vector< auto_ptr >…
user466534
3
votes
5 answers

'auto_ptr' and STL containers: writing an example of erroneous usage

This question raised after reading this tutorial: http://www.cprogramming.com/tutorial/auto_ptr.html There you can find the following statement: A subtle consequence of this behavior is that auto_ ptrs don't work well in all scenarios. For instance,…
nickolay
  • 3,643
  • 3
  • 32
  • 40
3
votes
5 answers

Sense of using std::auto_ptr

What is the sense of auto_ptr? Look at this code: #include #include class A { public: ~A() { std::cout << "DEST"; }; }; void func(A* pa) { std::cout << "A pointer"; } void test() { A a; …
Likon
  • 147
  • 1
  • 4
3
votes
2 answers

Auto Pointer constructor in VC2008

I have an auto pointer implementation: template class GAutoPtr { T *Ptr; public: typedef GAutoPtr &AutoPtrRef; GAutoPtr(T *ptr = 0) { Ptr = ptr; } GAutoPtr(AutoPtrRef p) …
3
votes
2 answers

std::auto_ptr Usage

I've read a reasonable amount in decent textbooks about the auto_ptr class. While I understand what it is, and how it gets you around the problem of getting exceptions in places like constructors, I am having trouble figuring out when someone would…
John Humphreys
  • 37,047
  • 37
  • 155
  • 255
3
votes
5 answers

C++ Is using auto_ptr references as out variables idiomatic?

Suppose I want to write factory method that is supposed to allocate heterogeneous objects on the heap and return them to the caller. I am thinking of designing the API like this: bool MakeEm(auto_ptr& outFoo, auto_ptr& outBar) { ... if…
Dilum Ranatunga
  • 13,254
  • 3
  • 41
  • 52
3
votes
2 answers

Doing type erasure safely without boost and c++0x

Say i have a templated class template class A; template<> class A { public: void print(){ std::cout << "I am an int !" << std::endl; } }; template<> class A { public: void print(){ std::cout << "I am…
Ragnar
  • 103
  • 6
3
votes
4 answers

std::auto_ptr error

For the below C++ code, I am getting an compiler error: class Mkt { int k; public: Mkt(int n): k(n) { throw; } ~Mkt() { cout<<"\n\nINSIDE Mkt DTOR function:\t"<
XMarshall
  • 953
  • 3
  • 11
  • 23
3
votes
4 answers

Why operator [] is not allowed on std::auto_ptr

Why operator [] is not allowed on std::auto_ptr? #include using namespace std ; template void foo( T capacity ) { auto_ptr temp = new T[capacity]; for( size_t i=0; i
Mahesh
  • 34,573
  • 20
  • 89
  • 115
3
votes
4 answers

Reference to a subset of a container object

i have a quick question about having a reference to a subset of a collection. Consider i have a vector of objects. Now I want to create another vector which is a subset of this vector, and I dont want to create a copy of the subset of objects. One…
Chenna V
  • 10,185
  • 11
  • 77
  • 104
3
votes
3 answers

C++ std::auto_ptr copy constructor

std::auto_ptr lacks const copy constructor, therefore I cannot use it directly in collections. is there some way to have for example vector of std::auto_ptr without using boost pointer collection template?
Anycorn
  • 50,217
  • 42
  • 167
  • 261
3
votes
3 answers

Why auto_ptr initialization using the assignment syntax is not allowed

I was reading through this book C++ standard library book And here is the part i can not understand: Note that class auto_ptr<> does not allow you to initialize an object with an ordinary pointer by using the assignment…
Vaska el gato
  • 198
  • 15
3
votes
1 answer

How is std::auto_ptr initialized with a rvalue?

#include #include #include #include std::string foo() { return std::string("yyyyyyyyyyyyy"); } void bar(std::string& s) { std::cout << s << std::endl; } std::auto_ptr foo1() { bool…
Ashot
  • 10,807
  • 14
  • 66
  • 117
3
votes
5 answers

auto_ptr and containers - C++

I'm currently working on a 2D game engine and I've read about auto_ptr's and how you should never put them in standard containers. My engine has this structure: StateManager -- has many --> State's. States are created and allocated in main, outside…
John
  • 2,571
  • 6
  • 32
  • 40
3
votes
5 answers

What is the correct way of using an auto_ptr on dynamically allocated arrays?

If i use auto_ptr to hold a pointer to a dynamically allocated array, when the auto_ptr gets killed it will use a plain delete operation and not delete[] thus not deleting my allocated array. How can i (properly) use auto_ptr on dynamically…
LoudNPossiblyWrong
  • 3,855
  • 7
  • 33
  • 45