I would like to be able to write a template function that can invoke a function call on all elements of a container. We can assume that the function name is always the same. However what isn't known is whether the container is holding objects or pointers. ie, whether I should de-reference.
template< typename TContainer >
void ProcessKeyedContainer( TContainer &keyedContainer )
{
for ( auto it = keyedContainer.begin(); it != keyedContainer.end(); ++it )
{
// do some random stuff here.
// ...
auto value = it->second;
value.Process(); // or value->Process() if the container has pointers
}
}
...
std::map< int, CMyObject > containerOfObjects;
containerOfObjects[0] = CMyObject();
std::map< int, CMyObject* > containerOfPointers;
containerOfPointers[0] = new CMyObject();
// I would like both calls to look near identical
ProcessKeyedContainer( containerOfObjects );
ProcessKeyedContainer( containerOfPointers );
Is there a neat way to be able to make the Process call inside ProcessKeyedContainer, without putting a burden on the caller ( ie the caller doesn't have to know to use it in one way for pointers and another way for objects ), and without having to duplicate too much code ?