Re: What am I missing? (template magic gone wrong)
This is how std::vector really looks like: std::vector<T, Alloc>. So
the call to ContainerDelete will indeed be ambigious.
In the first case container_type will be deduced to std::vector, K =
int* and V = std::allocator<int*>
And the second overload resolution will also find a proper match with
container_type = std:vector and T = int*.
This is why your code fails to compile (thank god there is a
compiler).
std::map look like this: map<Key, Data, Compare, Alloc>
So i changed the template parameters in your function:
template<template<typename, typename, typename, typename> class
container_type, typename K, typename V, typename C, typename A>
void ContainerDelete(container_type<K, V, C, A> &c) {
std::for_each(c.begin(), c.end(), AssociativeDelete());
c.clear();
}
You will not be able to call this function with std::set (set<Key,
Compare, Alloc>) (but that would not make sense anyway, becuase of the
delete p.second in AssociativeDelete)
And finally, one big misstake. You must preallocate the memory for
your std::vector if you want to be able to subscript in that way.
otherwhise, and which i recommend, use std::vector::push_back. (change
"my_vector[i] = new int();" to
"my_vector.push_back(new int()); ).
NB. considered using smart pointers like std::tr1::shared_ptr<T> ?
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]