473,396 Members | 1,849 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

mem_fun and template class function problems

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
1 2533
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Maitre Bart | last post by:
First, I describe my setup in 3 points. Then I describe what I want to do, and describe the error I get. 1) I have a container GTTable container pointer to data (class GT *). 2) I made a...
5
by: Old Wolf | last post by:
I have a member function that acts on an object. I would also like to have a member function that acts on a container of such objects, using std::for_each. I tried: #include <algorithm>...
2
by: Robbie Hatley | last post by:
I've got a function that I use a lot when making utility programs that need to do the same thing to every directory in a tree. Its prototype is: unsigned long int CursDirs (void Func(void)); ...
9
by: Mr X | last post by:
Can anyone tell how to fix the compile error for the program below? #include "iostream.h" #include <vector> #include <algorithm> #include <functional> #include <list> using namespace std;
13
by: Ioannis Vranos | last post by:
What is the exact difference between mem_fun and mem_fun_ref, since in all examples I looked at, they are used in exactly the same way? -- Ioannis Vranos http://www23.brinkster.com/noicys
4
by: ShaneG | last post by:
We have ptr_fun to handle functions, mem_fun to handle member functions that will be called through a pointer, and mem_fun_ref to handle member functions that will be called through a reference. ...
4
by: joseph cook | last post by:
I am getting a compile error on any compiler I try, so I know I have an error here. Can anyone see it? //includes class Foo { public: Foo(int a){m_hi = a;} int hi(){return m_hi;}
1
by: subramanian100in | last post by:
Consider the following program: #include <iostream> #include <string> #include <list> #include <algorithm> using namespace std; class Test
8
by: flopbucket | last post by:
Hi, I am having some problem using std::bind1st() and mem_fun. I want to bind member function calls to some kind of functor so it can be called later. The following works fine for me: ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.