each day, I am getting convinced the current STL algorithms (e.g:
for_each) are only useful in few or trivial cases
a good example is dealing with "private classes". let's imagine a
Collection class which is responsible for creating and owning
CollectionItem objects
so, making Collection a friend of CollectionItem allows me to declare
CollectionItem constructor and destructor private.
with that, I am sure only Collection will create and destroy
CollectionItem objects
so, I have something like that:
Collection ctor:
{
data.push_back(new CollectionItem);
....
}
Collection dtor:
{
for( int x = 0; x < data.size(); ++x ) {
delete data[x];
}
this works fine, but it won't compile if I use for_each in dtor
see:
/** generic pointer deleter */
template<typename T>
inline void deleter(T * ptr) { delete ptr; }
Collection dtor:
{
for_each(data.begin(),data.end(),&deleter<Collecti onItem>);
}
it only will compile if CollectionItem dtor is made public
do we have to wait until lambda or closure things appear? and will
they work in the case presented above?
Diego