473,624 Members | 2,245 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Operator overloading with templates

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 array.

I would like both + and += operators and for appropriate casting to be
handled. A minimal example of what I am trying to do is below (note
that the derived class holds only one value for simplicity).

The two + operators work fine with GCC (versions 3.4 and 4.1). But the
two += don't work when defined together (they work fine when defined
one at a time). When performing derived<L> += derived<R> the
operator+=(base <L,D>, R) is selected rather than operator+=(base <L,LD>,
base<R,RD>). Borland C++ Builder has this problem and a similar one
with the the binary addtion operator. I am really confused about GCC
because the operator prototypes are practically the same, so it would
seem that the the problem += and + operators are either both correct or
wrong.

Using operator+=(base <L,D>, L) works, but without automatic casting I
am after. Any suggestions on how to get around this problem, at least
in GCC would be great!

Example program (88 lines):
#include <iostream>

// curiously recurring base class
template <typename T, typename D>
class base
{
public:
T& operator[](std::size_t i) { return static_cast<D&> (*this)[i]; }
const T& operator[](std::size_t i) const { return static_cast<con st
D&>(*this)[i]; }

};

// curiously recurring derived class
template <typename T>
class derived : public base<T, derived<T> >
{
public:
derived(T t) : t_(t) {}
T& operator[](std::size_t i) {return t_; }
const T& operator[](std::size_t i) const {return t_; }
private:
T t_;
};
// type promotion trait
template <typename L, typename R>
class promote
{
};

template<>
class promote<int, double>
{
public:
typedef double type;
};
// binary addition operators with casting
template <typename L, typename D, typename R>
derived<typenam e promote<L,R>::t ype>
operator+ (const base<L, D>& l, const R& r)
{
std::cout << "base + literal" << std::endl;
typedef typename promote<L,R>::t ype P;
return derived<P>(stat ic_cast<P>(l[0]) + static_cast<P>( r));
}

template <typename L, typename DL, typename R, typename DR>
derived<typenam e promote<L,R>::t ype>
operator+ (const base<L, DL>& l, const base<R, DR>& r)
{
std::cout << "base + base" << std::endl;
typedef typename promote<L,R>::t ype P;
return derived<P>(stat ic_cast<P>(l[0]) + static_cast<P>( r[0]));
}
// self-assigned addition
template <typename L, typename D, typename R>
void
operator+= (base<L, D>& l, const R& r)
{
std::cout << "base += literal" << std::endl;
l[0] += static_cast<L>( r);
return;
}

template <typename L, typename DL, typename R, typename DR>
void
operator+= (base<L, DL>& l, const base<R, DR>& r)
{
std::cout << "base += base" << std::endl;
l[0] += static_cast<L>( r[0]);
return;
}
int main()
{
derived<int> d1(2);
derived<double> d2 = d1 + 3.; // base + literal
derived<double> d3 = d1 + d2; // base + base

d3 += 3.; // base += literal
d2 += d3; // base += base
}

Jun 28 '06 #1
2 2911
al*********@gma il.com wrote:
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 array.

I would like both + and += operators and for appropriate casting to be
handled. A minimal example of what I am trying to do is below (note
that the derived class holds only one value for simplicity).

The two + operators work fine with GCC (versions 3.4 and 4.1). But
the two += don't work when defined together (they work fine when
defined one at a time). When performing derived<L> += derived<R> the
operator+=(base <L,D>, R) is selected rather than
operator+=(base <L,LD>, base<R,RD>). Borland C++ Builder has this
problem and a similar one with the the binary addtion operator. I am
really confused about GCC because the operator prototypes are
practically the same, so it would seem that the the problem += and +
operators are either both correct or wrong.

Using operator+=(base <L,D>, L) works, but without automatic casting I
am after. Any suggestions on how to get around this problem, at least
in GCC would be great!

Example program (88 lines):
#include <iostream>

// curiously recurring base class
template <typename T, typename D>
class base
{
public:
T& operator[](std::size_t i) { return static_cast<D&> (*this)[i]; }
const T& operator[](std::size_t i) const { return static_cast<con st
D&>(*this)[i]; }

};

// curiously recurring derived class
template <typename T>
class derived : public base<T, derived<T> >
{
public:
derived(T t) : t_(t) {}
T& operator[](std::size_t i) {return t_; }
const T& operator[](std::size_t i) const {return t_; }
private:
T t_;
};
// type promotion trait
template <typename L, typename R>
class promote
{
};

template<>
class promote<int, double>
{
public:
typedef double type;
};
// binary addition operators with casting
template <typename L, typename D, typename R>
derived<typenam e promote<L,R>::t ype>
operator+ (const base<L, D>& l, const R& r)
{
std::cout << "base + literal" << std::endl;
typedef typename promote<L,R>::t ype P;
return derived<P>(stat ic_cast<P>(l[0]) + static_cast<P>( r));
}

template <typename L, typename DL, typename R, typename DR>
derived<typenam e promote<L,R>::t ype>
operator+ (const base<L, DL>& l, const base<R, DR>& r)
{
std::cout << "base + base" << std::endl;
typedef typename promote<L,R>::t ype P;
return derived<P>(stat ic_cast<P>(l[0]) + static_cast<P>( r[0]));
}
// self-assigned addition
template <typename L, typename D, typename R>
void
operator+= (base<L, D>& l, const R& r)
{
std::cout << "base += literal" << std::endl;
l[0] += static_cast<L>( r);
This doesn't compile for me when instantiated from

'd2 += d3'

'R' is 'derived<double >', 'L' is 'double'. There is no conversion
from 'derived<double >' to 'double'.

The existence of the other operator+= does not matter here, I am
guessing. The reason is that with 'd3' as the second argument
the compiler cannot deduce 'R' and 'DR' from 'derived<double >'.
return;
}

template <typename L, typename DL, typename R, typename DR>
void
operator+= (base<L, DL>& l, const base<R, DR>& r)
{
std::cout << "base += base" << std::endl;
l[0] += static_cast<L>( r[0]);
return;
}
int main()
{
derived<int> d1(2);
derived<double> d2 = d1 + 3.; // base + literal
derived<double> d3 = d1 + d2; // base + base

d3 += 3.; // base += literal
d2 += d3; // base += base
}


V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 29 '06 #2
After much searching I figured out why this problem is occuring....

If you look at the definition of the addition operators I gave in my
example
template <typename L, typename D, typename R>
derived<typenam e promote<L,R>::t ype>
operator+ (const base<L, D>& l, const R& r) template <typename L, typename DL, typename R, typename DR>
derived<typenam e promote<L,R>::t ype>
operator+ (const base<L, DL>& l, const base<R, DR>& r)
When the following commands are compiled derived<double> d2 = d1 + 3.; // base + literal this uses the operator+ (const base<L, D>& l, const R& r) as
expected...
derived<double> d3 = d1 + d2; // base + base however this can't use operator+ (const base<L, D>& l, const R& r) as
the return type is not defined so GCC eliminates it from the overload
resolution and finds operator+ (const base<L, DL>& l, const base<R,
DR>& r) as wanted. Borland C++ Builder doesn't seem to check the
return type.

For the += operators template <typename L, typename D, typename R>
void
operator+= (base<L, D>& l, const R& r) template <typename L, typename DL, typename R, typename DR>
void
operator+= (base<L, DL>& l, const base<R, DR>& r)
here the command d3 += 3.; // base += literal finds operator+= (base<L, D>& l, const R& r) as wanted but ...
d2 += d3; // base += base

also finds operator+= (base<L, D>& l, const R& r) instead of the wanted
operator+= (base<L, DL>& l, const base<R, DR>& r) as the first is a
"better" match (one less cast) and there isn't the return type problem.
Overloading resolution based on return type definition is entirely new
to me but from what I can find appears to be legal. Learn something
new every day and all...

Jun 29 '06 #3

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

Similar topics

51
3569
by: Jojo | last post by:
Is there any way to get to the left-hand side of an operator? Consider the following (this is not meant to be perfect code, just an example of the problem): class Matrix { public: int data; Matrix() {}
5
2054
by: Fabio Fracassi | last post by:
Hi, I belive what i am trying to do is not possible, but I hope someone can change my mind. Here is some code i'd like to write: template <class type> class engine1 {}; template <class type> class engine2 {};
6
5402
by: TuxC0d3 | last post by:
Hi! I'm diving into the some more ++ specific aspects of c++ (and finally accepting that c++ is more than "a plus added to c" :), so that means using namespaces, templates, std::strings, lists, vectors, operator overloading and what not.. And i was wondering if there is a way to override the global dereference operator, so to be able to check the address that one tries to dereference. This gives the ability to throw an exception when...
3
3450
by: toton | last post by:
Operator overloading has a sort syntax rather than member function call for stack based memory allocation. like complex<int> c1,c2,c3; c3= c1+c2; How the same can be applied to heap based memory allocation? like complex<int> * c1,*c2,*c3; i still want to do something like c3 = c1+c2 ; rether than *c3 = *c1+*c2;
5
3476
by: richard.parker | last post by:
Hello, I need to overload operator new with affecting the system libraries. Has anyone done this? I've got 2 static libraries and application source code where the operator needs to be overloaded, but I need to link with the system libraries (frameworks) and I DO NOT want my overloads to be mapped to them. I'm working with some code that is currently working on Win32 but also needs to work on the Mac. Under windows this is very...
11
1966
by: Zilla | last post by:
I have the following simple program. I just want to be able to do math operations (+, -, =)on Timer sublcasses, but want to handle cases where either rhs or lhs is an intrinsic value, However, the compile fails in my g++ 2.95 compiler during the 2nd to last line of the main() with template.cpp: In function `int main()': template.cpp:24: `operator +<int>(int, const int &)' must have an argument of class or enumerated type template.cpp:...
11
3804
by: jakester | last post by:
I am using Visual C++ 2007 to build the code below. I keep getting linkage error. Could someone please tell me what I am doing wrong? The code works until I start using namespace for my objects. Error 1 error LNK2019: unresolved external symbol "class std::basic_ostream<char,struct std::char_traits<char & __cdecl graph::operator<<(class std::basic_ostream<char,struct std::char_traits<char &,class graph::Node &)" (??6graph@@YAAAV?...
22
3605
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float v);
30
340
by: none | last post by:
I'm trying to overload the = operator using templates, but I have some problems with one of the overloads, I would like to make something like that: intvariable = fooclass; here's a pseudo code of what I'm doing: template <class COMPLEXTYPE> class foo
0
8233
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
8170
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8619
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8334
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
7158
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...
1
6108
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5561
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
4173
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1784
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.