K
k0tic
The intent of the following code is to call delete on each pointer in
the vector dead implicitly via auto_ptr<T>'s reset method semantics
which
delete their current pointer before taking ownership of reset's arg
in addition to when the object is destroyed -- in this case when,
as an automatic functor variable, the auto_ptr goes out of scope.
Is the following code legal and if not, why not.
Thanks in advance,
-L
#include <iostream>
#include <memory>
#include <algorithm>
#include <functional>
#include <vector>
using namespace std;
template<class T>
void
clear(vector<T *> t)
{
for_each(t.begin(), t.end(), mem_fun(&auto_ptr<T>::reset));
}
struct ToDie
{
~ToDie() { cout << "Dying..." << endl; };
};
int main(int argc, char **argv)
{
vector<ToDie *> dead;
dead.push_back(new ToDie());
clear(dead);
return 0;
}
Error:
/usr/include/c++/3.2/bits/stl_algo.h: In function `_Function
std::for_each(_InputIter, _InputIter, _Function) [with _InputIter =
__gnu_cxx::__normal_iterator<ToDie**, std::vector<ToDie*,
std::allocator<ToDie*> > >, _Function = std::mem_fun1_t<void,
std::auto_ptr<ToDie>, ToDie*>]':
auto_deleter.cpp:13: instantiated from `void clear(std::vector<T*,
std::allocator<T*> >) [with T = ToDie]'
auto_deleter.cpp:26: instantiated from here
/usr/include/c++/3.2/bits/stl_algo.h:157: no match for call to
`(std::mem_fun1_t<void, std::auto_ptr<ToDie>, ToDie*>) (ToDie*&)'
/usr/include/c++/3.2/bits/stl_function.h:653: candidates are: void
std::mem_fun1_t<void, _Tp, _Arg>:
perator()(_Tp*, _Arg) const [with
_Tp = std::auto_ptr<ToDie>, _Arg = ToDie*]
Compiler:
Reading specs from /usr/lib/gcc-lib/i586-mandrake-linux-gnu/3.2/specs
Configured with: ../configure --prefix=/usr --libdir=/usr/lib
--with-slibdir=/lib --mandir=/usr/share/man --infodir=/usr/share/info
--enable-shared --enable-threads=posix --disable-checking
--enable-long-long --enable-__cxa_atexit
--enable-languages=c,c++,ada,f77,objc,java
--host=i586-mandrake-linux-gnu --with-system-zlib
Thread model: posix
gcc version 3.2 (Mandrake Linux 9.0 3.2-1mdk)
the vector dead implicitly via auto_ptr<T>'s reset method semantics
which
delete their current pointer before taking ownership of reset's arg
in addition to when the object is destroyed -- in this case when,
as an automatic functor variable, the auto_ptr goes out of scope.
Is the following code legal and if not, why not.
Thanks in advance,
-L
#include <iostream>
#include <memory>
#include <algorithm>
#include <functional>
#include <vector>
using namespace std;
template<class T>
void
clear(vector<T *> t)
{
for_each(t.begin(), t.end(), mem_fun(&auto_ptr<T>::reset));
}
struct ToDie
{
~ToDie() { cout << "Dying..." << endl; };
};
int main(int argc, char **argv)
{
vector<ToDie *> dead;
dead.push_back(new ToDie());
clear(dead);
return 0;
}
Error:
/usr/include/c++/3.2/bits/stl_algo.h: In function `_Function
std::for_each(_InputIter, _InputIter, _Function) [with _InputIter =
__gnu_cxx::__normal_iterator<ToDie**, std::vector<ToDie*,
std::allocator<ToDie*> > >, _Function = std::mem_fun1_t<void,
std::auto_ptr<ToDie>, ToDie*>]':
auto_deleter.cpp:13: instantiated from `void clear(std::vector<T*,
std::allocator<T*> >) [with T = ToDie]'
auto_deleter.cpp:26: instantiated from here
/usr/include/c++/3.2/bits/stl_algo.h:157: no match for call to
`(std::mem_fun1_t<void, std::auto_ptr<ToDie>, ToDie*>) (ToDie*&)'
/usr/include/c++/3.2/bits/stl_function.h:653: candidates are: void
std::mem_fun1_t<void, _Tp, _Arg>:
_Tp = std::auto_ptr<ToDie>, _Arg = ToDie*]
Compiler:
Reading specs from /usr/lib/gcc-lib/i586-mandrake-linux-gnu/3.2/specs
Configured with: ../configure --prefix=/usr --libdir=/usr/lib
--with-slibdir=/lib --mandir=/usr/share/man --infodir=/usr/share/info
--enable-shared --enable-threads=posix --disable-checking
--enable-long-long --enable-__cxa_atexit
--enable-languages=c,c++,ada,f77,objc,java
--host=i586-mandrake-linux-gnu --with-system-zlib
Thread model: posix
gcc version 3.2 (Mandrake Linux 9.0 3.2-1mdk)