473,387 Members | 1,486 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 1184
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Kevin Christopher | last post by:
I've got a pile of code that won't compile for me, and can't figure out why. I'm either making a template / friend / operator overloading mistake (most likely - & why I'm asking here), or I'm...
4
by: Dave Theese | last post by:
Hello all, I'm trying to get a grasp of the difference between specializing a function template and overloading it. The example below has a primary template, a specialization and an overload. ...
5
by: | last post by:
Hi all, I've been using C++ for quite a while now and I've come to the point where I need to overload new and delete inorder to track memory and probably some profiling stuff too. I know that...
10
by: Christian Christmann | last post by:
Hi, I added an operator overloader in my template class: graph.h: template <class NODE> class Node {
3
by: Starx | last post by:
I was wondering if someone could help me get around this problem I'm having. I'm writing a fraction class and would like to overload the binary arithmetic operators so that when any numerical data...
16
by: WittyGuy | last post by:
Hi, What is the major difference between function overloading and function templates? Thanks! http://www.gotw.ca/resources/clcm.htm for info about ]
2
by: allan.mcrae | last post by:
I am having trouble with overloading the += operator when template parameters are used. I have a class holding an array (called "derived" in the following example) which derives from a base class...
2
by: brzozo2 | last post by:
Hello, this program might look abit long, but it's pretty simple and easy to follow. What it does is read from a file, outputs the contents to screen, and then writes them to a different file. It...
3
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.