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
6
votes
3 answers

Passing an auto_ptr to a function effectively makes it a sink. Why?

I'm reading some notes about shared pointers. They say the first attempt by STL with the auto_ptr had the following major drawbacks: They can't be used in STL containers Copying the auto_ptr transfers ownership Passing an auto_ptr to a function…
Freddie
  • 1,717
  • 2
  • 16
  • 23
6
votes
3 answers

Which kind of (auto) pointer to use?

I came accross several questions where answers state that using T* is never the best idea. While I already make much use of RIIC, there is one particular point in my code, where I use T*. Reading about several auto-pointers, I couldn't find one…
b.buchhold
  • 3,837
  • 2
  • 24
  • 33
6
votes
4 answers

Delete raw pointer argument to boost::bind

Lets say I have heap allocated A*, which I want to pass as argument to boost::bind. boost::bind is saved for later processing in some STL like container of boost::functions's. I want to ensure A* will be destroyed at destruction of the STL…
dimba
  • 26,717
  • 34
  • 141
  • 196
6
votes
5 answers

convert shared_ptr to auto_ptr?

I need to obtain auto_ptr from shared_ptr in my code. I can do reverse operation - convert auto_ptr to shared_ptr as shared_ptr has such constructor: template explicit shared_ptr(std::auto_ptr & r); Can I convert shared_ptr to auto_ptr?…
ks1322
  • 33,961
  • 14
  • 109
  • 164
6
votes
3 answers

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

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…
Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
6
votes
3 answers

Is get() reliable when an auto_ptr is uninitialized?

Consider the following code: std::auto_ptr p; if (p.get() == 0) { ... } Is the get() member function a standard and reliable way for checking that p has not been initialized? Will it always return 0, irrespective of the platform,…
Marc
  • 856
  • 1
  • 8
  • 20
6
votes
2 answers

The "most important const" vs. auto_ptr: Why the code does not compile?

The following code does not compile on Visual C++ 2008 nor 2010: #include struct A {}; std::auto_ptr foo() { return std::auto_ptr(new A); } const std::auto_ptr bar() { return std::auto_ptr(new A); } int main() { …
paercebal
  • 81,378
  • 38
  • 130
  • 159
6
votes
7 answers

Why does this code only print 42?

Could somebody please explain to me why does this code only print "42" instead of "created\n42"? #include #include #include using namespace std; class MyClass { public: MyClass() {cout<<"created"<
rlazo
  • 635
  • 4
  • 14
5
votes
5 answers

std::move vs std::auto_ptr?

What can I do with 'move' (r-value references) in C++11 what I can't with std::auto_ptr? (As I understand they are different implementations of one idea.) And old question again: is std::auto_ptr so bad component?
Iakov Minochkin
  • 432
  • 2
  • 10
5
votes
3 answers

Should I explicitly zero initialize auto_ptr?

Some of my colleagues prefer to explicitly initialize std::auto_ptr to 0 in constructor initialization list, but it will be initialized to 0 in it's constructor without any explicit initialization. So is there any reason to do it? #include…
ks1322
  • 33,961
  • 14
  • 109
  • 164
5
votes
6 answers

Letting go of auto_ptr

Occasionally, for fleeting moments, I think auto_ptr is cool. But most of the time I recognize that there are much simpler techniques that make it irrelevant. For example, if I want to have an object freed automatically, even if an exception is…
Brent Arias
  • 29,277
  • 40
  • 133
  • 234
5
votes
1 answer

auto_ptr Traps and Pitfalls

Besides all the known benefits of using auto_ptrs, what are auto_ptr "worst-practices"? Creating STL contrainers of auto_ptrs. auto_ptrs don't fulfill the 'CopyConstructable' requirement. See also Scott Meyer's "Effective STL", item 8. Creating…
Ralf Holly
  • 221
  • 1
  • 2
5
votes
4 answers

Code Review question - should I allow this passing of an auto_ptr as parameter?

Consider the following example code which I have recently seen in our code base: void ClassA::ExportAnimation(auto_ptr animation) { ... does something } // calling method: void classB::someMethod() { auto_ptr animation…
GarethOwen
  • 6,075
  • 5
  • 39
  • 56
5
votes
1 answer

Why does unique_ptr have an overload for auto_ptr?

I got a compiler error and noticed something interesting. For some reason unique_ptr has an overload for auto_ptr, but I thought auto_ptr was deprecated: /usr/local/include/c++/4.9.0/bits/unique_ptr.h:228:2: note:template
user4085715
  • 395
  • 2
  • 8
5
votes
2 answers

auto_ptr in a class not returning from a source function

Consider the following code: #include struct A { std::auto_ptr i; }; A F() { A a; return a; } int main(int argc, char **argv) { A a = F(); return 0; } When compiling I receive a compilation error, (see…
Cramer
  • 1,785
  • 1
  • 12
  • 20
1 2
3
13 14