templates / overloading operator()

M

Malcolm Smith

I have this functor:

struct DeleteIterObject
{
template< typename T >
void operator()(const T* ptr) const
{
delete ptr;
ptr = NULL;
}
};

Currently, the code is used like this:

FOwnerPolicy.DeleteObjects(FContainer.begin(), FContainer.end());

where FOwnerPolicy is a policy class of type 'TOwnIteratorObjectsPolicy',
shown below:

struct TOwnIteratorObjectsPolicy
{
template< class IterType >
void DeleteObjects(IterType IterB, IterType IterE)
{
std::for_each(IterB, IterE, DeleteIterObject());
}
};

Now I'm trying to work out how to specialize DeleteIterObject for the case
when 'T' is a std::pair< U*, V* >. I've created another functor that will
bind to either the first or the second element and delete the object - like
this for example:

struct TOwnPairSecondObjectsPolicy
{
template< class IterType >
void DeleteObjects(IterType IterB, IterType IterE)
{
std::for_each(IterB, IterE, BindPairSecond(DeleteIterObject()));
}
};

[ BindPairSecond is similar in design to Bind2nd, where I pass the .second
element to DeleteIterObject for processing ]

But I cannot work out how to create a specialized case of
DeleteIterObjects(). I would ideally like to convert this:

std::for_each(IterB, IterE, BindPairFirst(DeleteIterObject()));
std::for_each(IterB, IterE, BindPairSecond(DeleteIterObject()));

into a single call such as:

std::for_each(IterB, IterE, DeleteIterObject());

I'm sure it can be done by overloading the operator() method of
'DeleteIterObject' but I cannot work out the syntax.

Is what I'm after possible ?

--
 
P

Paul

Malcolm Smith said:
I have this functor:

struct DeleteIterObject
{
template< typename T >
void operator()(const T* ptr) const
{
delete ptr;
ptr = NULL;
}
};
Currently, the code is used like this:

FOwnerPolicy.DeleteObjects(FContainer.begin(), FContainer.end());

where FOwnerPolicy is a policy class of type 'TOwnIteratorObjectsPolicy',
shown below:

struct TOwnIteratorObjectsPolicy
{
template< class IterType >
void DeleteObjects(IterType IterB, IterType IterE)
{
std::for_each(IterB, IterE, DeleteIterObject());
}
};

Now I'm trying to work out how to specialize DeleteIterObject for the case
when 'T' is a std::pair< U*, V* >. I've created another functor that will
bind to either the first or the second element and delete the object - like
this for example:

struct TOwnPairSecondObjectsPolicy
{
template< class IterType >
void DeleteObjects(IterType IterB, IterType IterE)
{
std::for_each(IterB, IterE, BindPairSecond(DeleteIterObject()));
}
};

[ BindPairSecond is similar in design to Bind2nd, where I pass the .second
element to DeleteIterObject for processing ]

But I cannot work out how to create a specialized case of
DeleteIterObjects(). I would ideally like to convert this:

std::for_each(IterB, IterE, BindPairFirst(DeleteIterObject()));
std::for_each(IterB, IterE, BindPairSecond(DeleteIterObject()));

into a single call such as:

std::for_each(IterB, IterE, DeleteIterObject());

I'm sure it can be done by overloading the operator() method of
'DeleteIterObject' but I cannot work out the syntax.

Is what I'm after possible ?


Is this any help?

struct DeleteIterObject
{
template<typename T>
void operator()(const T)const{std::cout<<"normal()\n";}

template<typename T1,typename T2>
void operator()(const std::pair<T1,T2>)const{std::cout<<"specialized\n";}
};

Paul.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
474,176
Messages
2,570,947
Members
47,498
Latest member
log5Sshell/alfa5

Latest Threads

Top