auto-ptr

C++: auto_ptr + forward declaration?

依然范特西╮ 提交于 2019-12-04 00:02:51
问题 I have a class like this: class Inner; class Cont { public: Cont(); virtual ~Cont(); private: Inner* m_inner; }; in the .cpp, the constructor creates an instance of Inner with new and the destructor delete s it. This is working pretty well. Now I want to change this code to use auto_ptr so I write: class Inner; class Cont { public: Cont(); virtual ~Cont(); private: std::auto_ptr<Inner> m_inner; }; Now, the constructor initialized the auto_ptr and the destructor does nothing. But it doesn't

auto_ptr with swig

左心房为你撑大大i 提交于 2019-12-02 03:30:37
问题 I'm trying to wrap a C++ library which uses auto_ptr. I'm using swig and want to generate python bindings. I'v seen the section of the swig docu on how to use swig with smart pointers here. But I can't get it to work. Swig generates code that wants to initialize the auto_ptr using a const reference, but auto_ptr defines the copy constructor with a non-const reference e.g. auto_ptr(auto_ptr &). The generated code does not compile with "discards const qualifiers". When I manually delete the

auto_ptr with swig

╄→尐↘猪︶ㄣ 提交于 2019-12-02 00:57:39
I'm trying to wrap a C++ library which uses auto_ptr. I'm using swig and want to generate python bindings. I'v seen the section of the swig docu on how to use swig with smart pointers here . But I can't get it to work. Swig generates code that wants to initialize the auto_ptr using a const reference, but auto_ptr defines the copy constructor with a non-const reference e.g. auto_ptr(auto_ptr &). The generated code does not compile with "discards const qualifiers". When I manually delete the const qualifier the code compiles fine. I'v seen lots of mailing list entries but nothing helped. Can

Is there any reason to use auto_ptr?

≯℡__Kan透↙ 提交于 2019-12-01 03:04:15
After reading Jossutis' explanation on auto_ptr from his STL book I've got a strong impression that whatever task I would try to use it in I'd 100% fail becuase of one of many auto_ptr's pitfalls. My question is: are there any real life tasks where auto_ptr is really usefull and does fit there well? Clearly, auto_ptr looses against unique_ptr . Now, in a 'strict C++03 without boost' world, I use auto_ptr quite often, most notably : For 'factory member functions' which return a dynamically allocated instance of a given type : I like the fact that using std::auto_ptr in the return type explicits

C++: auto_ptr + forward declaration?

北城余情 提交于 2019-12-01 02:58:42
I have a class like this: class Inner; class Cont { public: Cont(); virtual ~Cont(); private: Inner* m_inner; }; in the .cpp, the constructor creates an instance of Inner with new and the destructor delete s it. This is working pretty well. Now I want to change this code to use auto_ptr so I write: class Inner; class Cont { public: Cont(); virtual ~Cont(); private: std::auto_ptr<Inner> m_inner; }; Now, the constructor initialized the auto_ptr and the destructor does nothing. But it doesn't work. the problem seem to arise when I'm instantiating this class. I get this warning: warning C4150:

Is it true that a unique_ptr declaration, unlike a auto_ptr declaration, is well-defined when its template type is of an incomplete type?

只愿长相守 提交于 2019-12-01 02:55:28
I wrote this article and got some comments on it that confused me. It basically boils down to my having seen T2 used only as a template parameter and mistakenly jumped to the conclusion that I could therefore take the opportunity of forward declaration: struct T2; struct T1 { std::auto_ptr<T2> obj; }; This invokes UB if I don't go on to define T2 somewhere in the same TU, because std::auto_ptr<T2> calls delete on its internal T2* , and calling delete on an pointer to an object of an incomplete type whose complete type has a non-trivial destructor is undefined : [C++11: 5.3.5/5]: If the object

Is it true that a unique_ptr declaration, unlike a auto_ptr declaration, is well-defined when its template type is of an incomplete type?

╄→尐↘猪︶ㄣ 提交于 2019-11-30 22:51:00
问题 I wrote this article and got some comments on it that confused me. It basically boils down to my having seen T2 used only as a template parameter and mistakenly jumped to the conclusion that I could therefore take the opportunity of forward declaration: struct T2; struct T1 { std::auto_ptr<T2> obj; }; This invokes UB if I don't go on to define T2 somewhere in the same TU, because std::auto_ptr<T2> calls delete on its internal T2* , and calling delete on an pointer to an object of an

Is there any reason to use auto_ptr?

守給你的承諾、 提交于 2019-11-30 22:13:55
问题 After reading Jossutis' explanation on auto_ptr from his STL book I've got a strong impression that whatever task I would try to use it in I'd 100% fail becuase of one of many auto_ptr's pitfalls. My question is: are there any real life tasks where auto_ptr is really usefull and does fit there well? 回答1: Clearly, auto_ptr looses against unique_ptr . Now, in a 'strict C++03 without boost' world, I use auto_ptr quite often, most notably : For 'factory member functions' which return a

Compilation problems with vector<auto_ptr<> >

假如想象 提交于 2019-11-29 10:22:16
Consider the following code: #include <iostream> #include <memory> #include <vector> using namespace std; struct A { int a; A(int a_):a(a_) {} }; int main() { vector<auto_ptr<A> > as; for (int i = 0; i < 10; i++) { auto_ptr<A> a(new A(i)); as.push_back(a); } for (vector<auto_ptr<A> >::iterator it = as.begin(); it != as.end(); ++it) cout << (*it)->a << endl; } When trying to compile it, I get the following obscure compiler error from g++: g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/proba.d" -MT"src/proba.d" -o"src/proba.o" "../src/proba.cpp" /usr/include/c++/4.1.2/ext/new_allocator

Why doesn't auto_ptr construction work using = syntax

扶醉桌前 提交于 2019-11-29 04:08:11
I ran into a compiler error that didn't make much sense to me: #include <memory> using namespace std; auto_ptr<Table> table = db->query("select * from t"); error: conversion from 'Table*' to non-scalar type 'std::auto_ptr< Table>' requested However, the following line does work: auto_ptr<Table> table(db->query("select * from t")); What is it about this definiton of the constructor that prevents it from working as I expect? I thought that initialized declarations used the constructors. Here's my auto_ptr 's constructor (from the SGI STL): explicit auto_ptr(element_type* __p = 0) throw() : _M