Re: Template friend (unary and binary) operators

From: Ruben Campos (Ruben.Campos_at_robotica.uv.es)
Date: 11/30/04


Date: Tue, 30 Nov 2004 16:44:03 +0100

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.mData) {}

    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?



Relevant Pages

  • template -> instance -> function unresolved ???
    ... friend template function accessing private data in the template. ... i compile this into a dll (yes i know, there is no exported function from ...
    (microsoft.public.vc.language)
  • Re: May a template argument be a friends of the template class?
    ... > diagnostic messages re ill-formed code, ... friend declaration of a class. ... If the identifier resolves to a typedef-name or a template ...
    (comp.lang.cpp)
  • Re: Friends and templates
    ... Because operator<< is a friend function to the Queue1 class (or at least it ... and friends are supposed to be able to access private ... template ...
    (alt.comp.lang.learn.c-cpp)
  • Re: template -> instance -> function unresolved ???
    ... friend template function accessing private data in the template. ... i compile this into a dll (yes i know, there is no exported function from ... template <class Element> ...
    (microsoft.public.vc.language)
  • Re: Why wont this compile
    ... Can someone explain why the following code won't compile in both VC7 and VC8 ... You haven't made the function a friend. ... friend void Func; ... template void Func; is a friend, not the function template void Func; which is presumably closer to what is desired. ...
    (microsoft.public.vc.language)