I have a bunch of containers of object pointers that I want to iterate through in different contexts to produce diagnostics for them. I'm struggling with the syntax required to define the functions... which, on account of these objects filtering through diverse parts of my application, seem best encapsulated in a dedicated diagnostics class thus:
// Code sketch only - detail fleshed out below...
class ObjectListDiagnoser
{
public:
static void GenerateDiagnostics( /* help required here! */ );
};
...
// Elsewhere in the system...
ObjectListDiagnoser::GenerateDiagnostics( /* help required here! */ );
What I'd like to be able to do (in places across my application) is at least this:
std::vector<MyObject *> objGroup1;
std::list<MyObject *> objGroup2;
ObjectListDiagnoser::GenerateDiagnostics( objGroup1.begin(), objGroup1.end() );
ObjectListDiagnoser::GenerateDiagnostics( objGroup2.begin(), objGroup2.end() );
ObjectListDiagnoser::GenerateDiagnostics( objGroup1.rbegin(), objGroup1.rend() );
I have tried to template my function in two ways, with no success:
class ObjectListDiagnoser
{
public:
// 1 - nope.
template <class ObjIter>
static void GenerateDiagnostics( ObjIter first, ObjIter last );
// 2. - nope.
template <class Container, class ObjIter>
static void GenerateDiagnostics( Container<MyObject *>::ObjIter first,
Container<MyObject *>::ObjIter last );
};
Can someone provide the correct syntax for this? The container type will vary, and the direction of iteration will vary, but always for the same type of object.
Summary of discussion in the comments below - case 1 is correct... but leads to a broadly unintelligible linker error if the template function definition is not in the header. template function definitions simply have to go in the header - a point easily forgotten. Slip it into the header, and all is well - compiles, links... and hopefully even runs.