473,695 Members | 2,724 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Template friend (unary and binary) operators

Some questions about this code:

template <typename T> class MyTemplate;
template <typename T> MyTemplate <T> operator- (const MyTemplate <T> &
object);
template <typename T> MyTemplate <T> operator- (const MyTemplate <T> &
object1, const MyTemplate <T> & object2);

template <typename T>
class MyTemplate
{
public:
MyTemplate (const T & data = T()) : mData(data) {}
MyTemplate (const MyTemplate <T> & object) : mData(object.mD ata) {}

MyTemplate <T> & operator= (const MyTemplate <T> & object) { mData =
object.mData; return *this; }

friend MyTemplate <T> operator- <T> (const MyTemplate <T> & object);
MyTemplate <T> & operator-= (const MyTemplate <T> & object) { mData -=
object.mData; return *this; }
friend MyTemplate <T> operator- <T> (const MyTemplate <T> & object1,
const MyTemplate <T> & object2);

private:

T mData;
};

template <typename T> MyTemplate <T> operator- (const MyTemplate <T> &
object) { return MyTemplate <T> (-object.mData); }
template <typename T> MyTemplate <T> operator- (const MyTemplate <T> &
object1, const MyTemplate <T> & object2) { MyTemplate <T> result(object1) ;
result -= object2; return result; }

int
main
{
MyTemplate <float> object1(1.0f);
MyTemplate <float> object2(-object1);
}

a) Trying to compile this wit MS Visual C++ 7.x returns me some syntax and
unexpected token errors, first, and then some "new definition" errors for
the friend operator-, saying me that it was previously defined as a member.
In fact, unary operator- is defined as a member, and subsequently binary
operator- is defined as a friend function. So, they are considered as the
same function by the compiler? This doesn't make sense for me, because they
have different parameters.

In order to deal with this, I've tried two alternatives: a) changing the
template MyTemplate <T> class for a non-template one, while keeping the
unary member and binary friend operator-, and b) keep MyTemplate <T> as a
template class, while changing member unary operator- for a friend one. In
both cases, these errors don't appear, and the code compiles (and works)
fine. So I know it is a fact which concern templates only. Is possible to
include a unary member operator- and a binary friend operator- in the same
template class?

b) Nothing to do with the previous question, first constructor have a
default parameter "const T & data = T()". When I instantiate MyTemplate
<float> objects through this constructor, both in the stack and in the heap,
mData is initialized to 0.0f value. Is this standard, being always all
built-in types initialized to zero when their default constructor (exists?)
is called (inside a template, for example)? Is this a coincidence, favoured
by the involved memory contents at the construction time? Or is this
compiler-dependent, implemented specifically by (in my case) MS Visual C++
7.x?
Jul 22 '05 #1
5 2633
Ruben Campos wrote:
Some questions about this code:

template <typename T> class MyTemplate;
template <typename T> MyTemplate <T> operator- (const MyTemplate <T> &
object);
template <typename T> MyTemplate <T> operator- (const MyTemplate <T> &
object1, const MyTemplate <T> & object2);

template <typename T>
class MyTemplate
{
public:
MyTemplate (const T & data = T()) : mData(data) {}
MyTemplate (const MyTemplate <T> & object) : mData(object.mD ata) {}

MyTemplate <T> & operator= (const MyTemplate <T> & object) { mData =
object.mData; return *this; }

friend MyTemplate <T> operator- <T> (const MyTemplate <T> & object);
MyTemplate <T> & operator-= (const MyTemplate <T> & object) { mData -=
object.mData; return *this; }
friend MyTemplate <T> operator- <T> (const MyTemplate <T> & object1,
const MyTemplate <T> & object2);

private:

T mData;
};

template <typename T> MyTemplate <T> operator- (const MyTemplate <T> &
object) { return MyTemplate <T> (-object.mData); }
template <typename T> MyTemplate <T> operator- (const MyTemplate <T> &
object1, const MyTemplate <T> & object2) { MyTemplate <T> result(object1) ;
result -= object2; return result; }

int
main
{
MyTemplate <float> object1(1.0f);
MyTemplate <float> object2(-object1);
}

a) Trying to compile this wit MS Visual C++ 7.x returns me some syntax and
unexpected token errors, first, and then some "new definition" errors for
the friend operator-, saying me that it was previously defined as a member.
It compiles just fine with gcc 3.3.3 (after adding parantheses after
main), so I suppose that there's nothing wring with the code as such.
b) Nothing to do with the previous question, first constructor have a
default parameter "const T & data = T()". When I instantiate MyTemplate
<float> objects through this constructor, both in the stack and in the heap,
mData is initialized to 0.0f value. Is this standard, being always all
built-in types initialized to zero when their default constructor (exists?)
is called (inside a template, for example)? Is this a coincidence, favoured
by the involved memory contents at the construction time? Or is this
compiler-dependent, implemented specifically by (in my case) MS Visual C++
7.x?


I don't believe the standard says anything about implicitly initializing
variables, so (without reading the standard) I'd say it's
compiler-dependent.

/ martin
Jul 22 '05 #2

"Martin Magnusson" <martin@-xx-blecket-xx-.org> escribió en el mensaje
news:1101811069 .OMD19JDIzON1F4 45jEDYbw@terane ws...
Ruben Campos wrote:
Some questions about this code:

template <typename T> class MyTemplate;
template <typename T> MyTemplate <T> operator- (const MyTemplate <T> &
object);
template <typename T> MyTemplate <T> operator- (const MyTemplate <T> &
object1, const MyTemplate <T> & object2);

template <typename T>
class MyTemplate
{
public:
MyTemplate (const T & data = T()) : mData(data) {}
MyTemplate (const MyTemplate <T> & object) : mData(object.mD ata) {}

MyTemplate <T> & operator= (const MyTemplate <T> & object) { mData =
object.mData; return *this; }

friend MyTemplate <T> operator- <T> (const MyTemplate <T> & object);
MyTemplate <T> & operator-= (const MyTemplate <T> & object) {
mData -= object.mData; return *this; }
friend MyTemplate <T> operator- <T> (const MyTemplate <T> & object1,
const MyTemplate <T> & object2);

private:

T mData;
};

template <typename T> MyTemplate <T> operator- (const MyTemplate <T> &
object) { return MyTemplate <T> (-object.mData); }
template <typename T> MyTemplate <T> operator- (const MyTemplate <T> &
object1, const MyTemplate <T> & object2) { MyTemplate <T>
result(object1) ; result -= object2; return result; }

int
main
{
MyTemplate <float> object1(1.0f);
MyTemplate <float> object2(-object1);
}

a) Trying to compile this wit MS Visual C++ 7.x returns me some syntax
and unexpected token errors, first, and then some "new definition" errors
for the friend operator-, saying me that it was previously defined as a
member.
It compiles just fine with gcc 3.3.3 (after adding parantheses after
main), so I suppose that there's nothing wring with the code as such.


Sorry, it's only a transcription error. It should appear as:

int
main (int argn, char ** argc)

The errors I've described are related with the template, and its friend
functions. They appear with MS Visual C++ 7.1.3088, even when enabling the
Microsoft language extensions (which I don't want to keep enabled).
b) Nothing to do with the previous question, first constructor have a
default parameter "const T & data = T()". When I instantiate MyTemplate
<float> objects through this constructor, both in the stack and in the
heap, mData is initialized to 0.0f value. Is this standard, being always
all built-in types initialized to zero when their default constructor
(exists?) is called (inside a template, for example)? Is this a
coincidence, favoured by the involved memory contents at the construction
time? Or is this compiler-dependent, implemented specifically by (in my
case) MS Visual C++ 7.x?


I don't believe the standard says anything about implicitly initializing
variables, so (without reading the standard) I'd say it's
compiler-dependent.

/ martin

Jul 22 '05 #3
Sorry. I included the code corresponding to the first compilable variation
code, that with unary operator- as a friend function. So tell me repeat my
first message, now corrected. Sorry, Martin Magnusson, for this (all mine)
mistake.

Some questions about this code:

template <typename T> class MyTemplate;
template <typename T> MyTemplate <T> operator- (const MyTemplate <T> &
object1, const MyTemplate <T> & object2);

template <typename T>
class MyTemplate
{
public:
MyTemplate (const T & data = T()) : mData(data) {}
MyTemplate (const MyTemplate <T> & object) : mData(object.mD ata) {}

MyTemplate <T> & operator= (const MyTemplate <T> & object) { mData =
object.mData; return *this; }

MyTemplate <T> operator- (const MyTemplate <T> & object) { return
MyTemplate <T> (-mData); }
MyTemplate <T> & operator-= (const MyTemplate <T> & object) { mData -=
object.mData; return *this; }
friend MyTemplate <T> operator- <T> (const MyTemplate <T> & object1,
const MyTemplate <T> & object2);

private:
T mData;
};

template <typename T> MyTemplate <T> operator- (const MyTemplate <T> &
object1, const MyTemplate <T> & object2) { MyTemplate <T> result(object1) ;
result -= object2; return result; }

int
main (int argn, char ** argc)
{
MyTemplate <float> object1(1.0f);
MyTemplate <float> object2(-object1);
}

a) Trying to compile this wit MS Visual C++ 7.x returns me some syntax and
unexpected token errors, first, and then some "new definition" errors for
the binary friend operator-, saying me that it was previously defined as a
member. In fact, unary operator- is defined as a member, and subsequently
binary operator- is defined as a friend function. So, are they considered
the same function by the compiler? This doesn't make sense for me, because
they have different parameters.

In order to deal with this, I've tried two alternatives: a) changing the
template MyTemplate <T> class for a non-template one, while keeping the
unary member and binary friend operator-, and b) keep MyTemplate <T> as a
template class, while changing member unary operator- for a friend one. In
both cases, these errors don't appear, and the code compiles (and works)
fine. So I know it is a fact which concern templates only. Is possible to
include a unary member operator- and a binary friend operator- in the same
template class?

NOTE: in a real case, I use to include all the implementations in a separate
..cpp file (which, in the case of template classes, is #included in the .h).
For readability, and for short, I included here implementation of member
functions directly inside template class declaration, but I kept the
implementation of the friend function outside, in order to reproduce the
error situation. I've tried this code, however, and it really returns me
errors which I've mentioned.

b) Nothing to do with the previous question, first constructor have a
default parameter "const T & data = T()". When I instantiate MyTemplate
<float> objects through this constructor, both in the stack and in the heap,
mData is initialized to 0.0f value. Is this standard, being always all
built-in types initialized to zero when their default constructor (exists?)
is called (inside a template, for example)? Is this a coincidence, favoured
by the involved memory contents at the construction time? Or is this
compiler-dependent, implemented specifically by (in my case) MS Visual C++
7.x?
Jul 22 '05 #4
"Ruben Campos" <Ru**********@r obotica.uv.es> wrote in message news:<co******* ***@peque.uv.es >...
Some questions about this code:
[snip]
int
main main () {
MyTemplate <float> object1(1.0f);
MyTemplate <float> object2(-object1);
}


With the fix for main compiles just fine with VC++ 7.1

/dan
Jul 22 '05 #5
Please, see my last (and corrected) message in this thread. Sorry for the
mistake, and thank you for your time.

"Dan Cernat" <dc*****@excite .com> escribió en el mensaje
news:aa******** *************** ***@posting.goo gle.com...
"Ruben Campos" <Ru**********@r obotica.uv.es> wrote in message
news:<co******* ***@peque.uv.es >...
Some questions about this code:


[snip]
int
main

main ()
{
MyTemplate <float> object1(1.0f);
MyTemplate <float> object2(-object1);
}


With the fix for main compiles just fine with VC++ 7.1

/dan

Jul 22 '05 #6

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

Similar topics

8
3821
by: Elementary Penguin | last post by:
Is there a left add unary operator ( or do I have to overload one ) ? For example: string s1 = "A"; s1 += "B"; Console.WriteLine( s1 )
5
2126
by: Teddy | last post by:
Hello all consider the class Date declaretion below: class Date { public: Date(); Date(int year, int month, int day); Date(const string&); int getYear() const;
4
1896
by: desalvo123 | last post by:
This compile and works fine. Example 1: --------------------- class MyDouble { public: MyDouble(double val) { m_Value = val; }; friend MyDouble operator + (const MyDouble&, const MyDouble&);
13
1637
by: jsnX | last post by:
say i have a function object silly that takes a const ref to clowns class silly : public std::unary_function<const clown&, bool> { ... } and then i decide to feed it a bunch of pointers to clowns: for_each(clown_pointers.begin(), clown_pointers.end(), silly());
17
3496
by: Anoob | last post by:
Can we consider () unary operator when calling a function, in exps eq. f(), ( 1 + 2). But when we call function f( 10 ) it is a binary operator. Even if we pass f( 10, 20) as we are using , operator there one operand will be there for ()? And Unary operators like !, +, -, ~, ... are said to be having associativity right to left which means that we can write, (but not allowed) 1. 2! 2. !2
3
3755
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and thawed it out. I built a console app using Microsoft Visual C++ 6 (VC++) and it worked great. Only one line in the header file had to be commented out. I built a console app using Borland C++ Builder 5. The linker complained of references to...
1
1460
by: Helmut Jarausch | last post by:
Hi, could anybody please explain to me what's wrong with the following code template <typename INT,INT Pclass Zp; template <typename INT,INT P> Zp<INT,Poperator-(const Zp<INT,P>& a, const Zp<INT,P>& b);
9
2200
by: neildferguson | last post by:
I am using templates with a little project I am working on. My compiler (GCC) is finding a particular construct ambiguous. Can anyone suggest something I might change in the declaration of class Length so that I can use operator+ the way I'd like? //========================================= // File lentest.h: #ifndef FDIMENS_LENGTH_INCL #define FDIMENS_LENGTH_INCL
16
4702
by: JoseMariaSola | last post by:
How may operators and operands does (typename) expression has? I'd say one operator, the cast operator, and two operands: typename and expression. But everywhere I read, cast is categorized as an unary operator. Why is that? Is it just a syntax cotegory? Thanks.
0
8649
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
8586
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
9137
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
8864
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
8838
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
7683
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
5842
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
4592
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3025
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 we have to send another system

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.