Template friend (unary and binary) operators

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


Date: Tue, 30 Nov 2004 10:54:33 +0100

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

    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?



Relevant Pages

  • Re: Word 2007 Templates collection
    ... Dim myTemplate As Template ... It seems to me that it should but I recall fighting this beast before, and the only way to get it to work was to look through the Templates collection one template at a time. ...
    (microsoft.public.word.vba.general)
  • Re: Toggle macro button
    ... Providing the CommandBar belongs to a Template you can (using your ... > Dim SavedState As Boolean ... > Dim ControlTemplate As Template ... myTemplate As Template ...
    (microsoft.public.word.vba.beginners)
  • Re: Toggle macro button
    ... Klaus Linke wrote: ... > If you don't know the template you'll put the code in (or want to be ... myTemplate As Template ... Unfortunately (and dimming may result ...
    (microsoft.public.word.vba.beginners)
  • Re: Passing data to word template using VBScript
    ... Open the template and then VBA editor. ... Create public methods and/or properties within MyTemplate and you would be ... "bchodo" wrote in message ...
    (microsoft.public.word.vba.general)
  • Re: Create Group Member
    ... Your question sounds there are other templates other than the .eml template. ... Now you will have an envelope icon on your Desktop that when you click on it, you will have a New Message window with your saved opening & member list ready to go. ... I would suggest putting your email address in the To line to keep the Undisclosed Recipients from showing instead which may be filtered upon. ...
    (microsoft.public.windows.inetexplorer.ie6_outlookexpress)