473,468 Members | 1,371 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Multiple operator() overloading for functor object

Is it possible to overload multiple times operator() ? For example for
using one or two arguments ? My visual c++ does not seem to like it :

I was trying to implement an arbitrary integer precision class (I know
such libraries already exist) and I wrote the following piece of code
:

template <typename T> class plus_with_carry {
public:
plus_with_carry(void) : carry(false) {};
T operator() (const T& left, const T& right) {
T temp = left + right + ((carry) ? 1 : 0);
carry = (temp<left);
};
T operator() (const T& arg) {
T temp = arg + ((carry) ? 1 : 0);
carry = (temp<arg);
};
bool carry;
};
to be used as a functor in the CBigInt operator+ (not yet complete) :

const CBigInt operator+ (const CBigInt& left, const CBigInt&
right) {
CBigInt temp;
if (left.sign==right.sign) {
std::vector<typename CBigInt::T>::size_type
sizel =
left.values.size();
std::vector<typename CBigInt::T>::size_type
sizer =
right.values.size();
temp.sign=left.sign;
plus_with_carry<typename CBigInt::T> myplus();
if (sizel<sizer) {
temp.values.reserve(sizer);
std::transform(left.values.begin(),
left.values.end(),
right.values.begin(),
std::back_inserter(temp.values), myplus);

std::transform(right.values.begin()+sizel, right.values.end(),

std::back_inserter(temp.values), myplus);
} else {
temp.values.reserve(sizel);
std::transform(right.values.begin(),
right.values.end(),
left.values.begin(),
std::back_inserter(temp.values), myplus);

std::transform(left.values.begin()+sizer, left.values.end(),

std::back_inserter(temp.values), myplus);
}
} else {
}
return temp;
}

but my compiler does not find the binary version of the functor
plus_with_carry...

Is this a problem with my way of writing the functor ? Is this allowed
by the standard ?

Thanks in advance for your help,

Charles
Jul 22 '05 #1
2 2374
Charles-Antoine Giuliani wrote in news:kkauk0p5v6kknovdd4v8669ri6lrd377nl@
4ax.com in comp.lang.c++:

Is it possible to overload multiple times operator() ? For example for
using one or two arguments ? My visual c++ does not seem to like it :


Once I reformated you code (*) I found:

plus_with_carry<typename CBigInt::T> myplus();

This is a function declaration:

myplus a function taking no arguments and returning a
plus_with_carry<typename CBigInt::T>

Loose the parenthesis.

HTH.

*) Most newsreaders wrap at 76 char's, when I got you code
operator + was a complete mess.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #2
Thanks a lot for this fast answer, it does the trick ! Pretty stupid
from me ;-)

This was my first time posting on the newsgroup, so pardon me for the
bad formatting...

Charles

On 20 Sep 2004 19:37:11 GMT, Rob Williscroft <rt*@freenet.co.uk>
wrote:
Charles-Antoine Giuliani wrote in news:kkauk0p5v6kknovdd4v8669ri6lrd377nl@
4ax.com in comp.lang.c++:

Is it possible to overload multiple times operator() ? For example for
using one or two arguments ? My visual c++ does not seem to like it :


Once I reformated you code (*) I found:

plus_with_carry<typename CBigInt::T> myplus();

This is a function declaration:

myplus a function taking no arguments and returning a
plus_with_carry<typename CBigInt::T>

Loose the parenthesis.

HTH.

*) Most newsreaders wrap at 76 char's, when I got you code
operator + was a complete mess.

Rob.


Jul 22 '05 #3

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

Similar topics

2
by: Malcolm Smith | last post by:
I have this functor: struct DeleteIterObject { template< typename T > void operator()(const T* ptr) const { delete ptr; ptr = NULL; }
3
by: Johan | last post by:
Hi, What does overloading operator () exactly do ?. John
20
by: Manuel | last post by:
Hi. Before all, please excuse me for bad english and for very newbie questions. I hope to don't boring you. I'm trying to write a very simple GUI using openGL. So I'm writing some different...
1
by: sarathy | last post by:
Hi, I have few doubts in operator overloading. Overloading is fine for operators such as +, * , - , / ....... But why would anyone overload operators ( ) -I dont see any real use when these are...
7
by: John | last post by:
I always understood that in C++, if I said a + b a.operator+(b) is called. Now this makes sense with the operator<< when used in the following way cout << 100; // converts to ...
3
by: Eric Lilja | last post by:
Hello group! I tried to make a simple program to exhibit why making a operator() in a functor a member template is a good idea, but I can't get this code to compile, hehe. I've reduced it to the...
2
by: aaragon | last post by:
Hi everyone, Can someone point me out why I can't declare the operator() of a functor as static? The reason behind this is that I want to be able to call to the function without instantiating...
3
by: dizzy | last post by:
Hi I wonder if this code is standard conformant and should work on all conformant implementations (for some type T): 1: void* mem = ::operator new(sizeof(T)); 2: T* p = new(mem) T(args...);...
9
by: fgh.vbn.rty | last post by:
I am frequently using maps like the following: map<string, map<int, vector< pair<int, int m1; map<int, map<string, map<int, int m2; This can be a little difficult to maintain if another coder...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
1
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...
0
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,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.