473,785 Members | 2,851 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

templates / overloading operator()

I have this functor:

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

Currently, the code is used like this:

FOwnerPolicy.De leteObjects(FCo ntainer.begin() , FContainer.end( ));

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

struct TOwnIteratorObj ectsPolicy
{
template< class IterType >
void DeleteObjects(I terType IterB, IterType IterE)
{
std::for_each(I terB, IterE, DeleteIterObjec t());
}
};

Now I'm trying to work out how to specialize DeleteIterObjec t 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 TOwnPairSecondO bjectsPolicy
{
template< class IterType >
void DeleteObjects(I terType IterB, IterType IterE)
{
std::for_each(I terB, IterE, BindPairSecond( DeleteIterObjec t()));
}
};

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

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

std::for_each(I terB, IterE, BindPairFirst(D eleteIterObject ()));
std::for_each(I terB, IterE, BindPairSecond( DeleteIterObjec t()));

into a single call such as:

std::for_each(I terB, IterE, DeleteIterObjec t());

I'm sure it can be done by overloading the operator() method of
'DeleteIterObje ct' 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 1207
"Malcolm Smith" <mj***********@ optusnet.com.au > wrote in message news:<41******* *************** *@news.optusnet .com.au>...
I have this functor:

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

FOwnerPolicy.De leteObjects(FCo ntainer.begin() , FContainer.end( ));

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

struct TOwnIteratorObj ectsPolicy
{
template< class IterType >
void DeleteObjects(I terType IterB, IterType IterE)
{
std::for_each(I terB, IterE, DeleteIterObjec t());
}
};

Now I'm trying to work out how to specialize DeleteIterObjec t 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 TOwnPairSecondO bjectsPolicy
{
template< class IterType >
void DeleteObjects(I terType IterB, IterType IterE)
{
std::for_each(I terB, IterE, BindPairSecond( DeleteIterObjec t()));
}
};

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

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

std::for_each(I terB, IterE, BindPairFirst(D eleteIterObject ()));
std::for_each(I terB, IterE, BindPairSecond( DeleteIterObjec t()));

into a single call such as:

std::for_each(I terB, IterE, DeleteIterObjec t());

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

Is what I'm after possible ?

Is this any help?

struct DeleteIterObjec t
{
template<typena me T>
void operator()(cons t T)const{std::co ut<<"normal()\n ";}

template<typena me T1,typename T2>
void operator()(cons t std::pair<T1,T2 >)const{std::co ut<<"specialize d\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 DeleteIterObjec t
{
template<typena me T>
void operator()(cons t T)const{std::co ut<<"normal()\n ";}

template<typena me T1,typename T2>
void operator()(cons t std::pair<T1,T2 >)const{std::co ut<<"specialize d\n";} };

Paul.

Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1128
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 using something not supported by g++ 2.96 (less likely). The big picture: I'm writing a wrapper class template that is supposed to wrap around a primitive data type so that I can send myself notifications whenever some code changes that data type...
4
6487
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. Note that the overload is identical to the specialization except, of course, for the missing "template <>". I don't know if my questions will be a bit too broad or not, but I thought I'd give it shot... When is overloading preferable to...
5
5247
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 discussions of new and delete is a pretty damn involved process but I'll try to stick to the main information I'm looking for currently. I've searched around for about the last too weeks and have read up on new and overloading it to some extent but...
10
1909
by: Christian Christmann | last post by:
Hi, I added an operator overloader in my template class: graph.h: template <class NODE> class Node {
3
2453
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 type (int, double, long double, etc.) is used with a fraction it is first converted to a fraction and then added, the result being a fraction. So I started with this: template <class numericType> friend fraction operator +(const fraction&...
16
16291
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
2920
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 ("base"). I want to be able to add: 1) any derived array holding class to any other derived array holding class 2) any derived array holding class to a literal value (e.g int, double, etc) for which addition is defined for the type in the...
2
2261
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 uses map<and heavy overloading. The problem is, the output file differs from input, and for the love of me I can't figure out why ;p #include <iostream> #include <fstream> #include <sstream>
3
3283
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, obj2 ;
0
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10324
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10090
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9949
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8971
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6739
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.