Multiple operator() overloading for functor object

  • Thread starter Charles-Antoine Giuliani
  • Start date
C

Charles-Antoine Giuliani

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
 
R

Rob Williscroft

Charles-Antoine Giuliani wrote in 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.
 
C

Charles-Antoine Giuliani

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
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top