By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,562 Members | 1,244 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,562 IT Pros & Developers. It's quick & easy.

mem_fun and template class function problems

P: n/a
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>::operator()(_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)
Jul 19 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On 3 Sep 2003 21:38:40 -0700, ln****@netscape.net (k0tic) wrote:
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[i];
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;
}

Jul 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.