Re: What am I missing? (template magic gone wrong)
In article <Pine.GHP.4.58.0802271335370.21487@stud3.tuwien.ac.at>,
Sebastian Redl <e0226430@student.tuwien.ac.at> wrote:
\
template <typename K, typename V, typename Allocator = std::allocator
[, futher, implementation-defined parameters with defaults]>
class map;
Wrong. the standard no longer allows implementation defined
parameters to the template declaration. this was an early fix to the
standard. therefore you can handle containers if you have a 21th
century compiler.
It is simpler to have one functor class/struct and overloaded
templated operator ()'s.
struct MemberDeleter
{
template <class T>
void operator () (T &x) { delete x;}
template <class T1,class T2>
void operator () (std::pair<T1,T2> &x)
{ delete x.second;}
};
is an example
beware the that the container contains invalid ptrs after the for_each
operation , safer is to transform the contents so the ptrs are now null
ptrs (== 0) ,but not sure if it is really needed.
Easier to maintain is to store smart ptrs in containers then you won't
be bit by
std::remove or remove_if overwritting the ptrs as it would do in most
implementations of remove[if] and sequence mutatining algorithms.
struct ptr_points_to
{
int a;
ptr_points_to(int x):a(a){}
bool operator () (int *x) const
{
return *x == a;
}
}
std::vector<int *> ptrs;
for(int i=0;i!=10;++i)
ptrs.push_back(new int(i % 3));
std::vector<int>::iterator it = std::remove_if
(ptrs.begin(),ptrs.end(),ptr_points_to(2));
now some of the ptrs are dangling , since the entries
of [ptrs.begin(), it ) are overwritten!!! in remove_if.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]