mem_fun and template class function problems

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>::eek: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)
 
A

Alf P. Steinbach

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

The reset method of an auto_ptr works on the auto_ptr object.

You don't have any auto_ptr objects.

Use simple solutions to simple problems.



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
No.


and if not, why not.

See above.


#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;
}


#include <iostream>
#include <vector>

template<class T>
void clear( std::vector< T* >& t )
{
for( size_t i = 0; i < t.size(); ++i ) delete t;
t.clear();
}

struct ToDie
{
~ToDie() { std::cout << "Dying..." << std::endl; };
};

int main()
{
std::vector<ToDie *> dead;
dead.push_back( new ToDie() );
clear( dead );
std::cout << static_cast<unsigned long>( dead.size() ) << std::endl;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,832
Latest member
GlennSmall

Latest Threads

Top