468,315 Members | 1,414 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,315 developers. It's quick & easy.

templates / overloading operator()

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 ?

--
---
Malcolm Smith
MJ Freelancing
http://www.mjfreelancing.com
Borland Technology Partner
Jul 22 '05 #1
2 1064
"Malcolm Smith" <mj***********@optusnet.com.au> wrote in message news:<41***********************@news.optusnet.com. au>...
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.
Jul 22 '05 #2
My first attempt was similar. I'll keep playing.

--
---
Malcolm Smith
MJ Freelancing
http://www.mjfreelancing.com
Borland Technology Partner


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.

Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Kevin Christopher | last post: by
4 posts views Thread by Dave Theese | last post: by
5 posts views Thread by | last post: by
10 posts views Thread by Christian Christmann | last post: by
3 posts views Thread by Starx | last post: by
2 posts views Thread by allan.mcrae | last post: by
3 posts views Thread by y-man | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.