Topic: Implicit operation relationships


Author: jjb@watson.ibm.com (John Barton)
Date: 1995/11/07
Raw View
In article <47dn87$a17@sunsystem5.informatik.tu-muenchen.de>,
schuenem@informatik.tu-muenchen.de (Ulf Schuenemann) writes:
|> In article <46vaga$dng@marina.cinenet.net>, scotty@cinenet.net (J Scott
|> Peter XXXIII I/III) writes:
|> |> a += b =>  a = a + b
|> |> a + b =>  tmp = a; a + b; tmp
|> |> Similarly for other composite operators.
|> |>
|> |> a != b => !(a == b) and vice versa
|> |> a >= b => !(a < b) etc
|> |> a < b => a - b < 0 etc
|> |> a - b => a + (-b) and vice versa
|> |> -b  => 0 - b
|> |> +b  => b
|> |> a++  => a += 1 also --
|> |> a->b  =>  (*a).b
|>
|> The standard header <utility> (20.2.1) already defines template-
|> operators for the relational operators, deducing them to == and <
|> a != b  => !(a == b)
|> a >  b  =>   b < a
|> a <= b  => !(b < a)
|> a >= b  => !(a < b)
|>
|> You can extend this idea:
|>
|> template<class T> T& operator+=(T&a, T&b) { return a = a + b; }
|> template<class T> T  operator- (T&a, T&b) { return a + (-b); }
|> template<class T> T  operator- (T&a)      { return 0 - a; }
|> template<class T> T& operator+ (T&a)      { return a; }
|> template<class T> T& operator++(T&a)      { return a += 1; }
|> template<class T> T& operator++(T&a,int)  { return ++a; }

This will not work: it prevent you from writing any templates
for these operators over types that do not have "1" or "0"
or that need a definition other than the one above.

--
John.

John J. Barton        jjb@watson.ibm.com            (914)784-6645
 <http://www.research.ibm.com/xw-SoftwareTechnology>
H1-C13 IBM Watson Research Center P.O. Box 704 Hawthorne NY 10598
---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]





Author: rp@iscp.bellcore.com (Robert Pearlman)
Date: 1995/11/07
Raw View
In article <47l76s$qu1@watnews2.watson.ibm.com>, jjb@watson.ibm.com (John Barton) writes:
|> In article <47dn87$a17@sunsystem5.informatik.tu-muenchen.de>,
|> schuenem@informatik.tu-muenchen.de (Ulf Schuenemann) writes:
|> |> In article <46vaga$dng@marina.cinenet.net>, scotty@cinenet.net (J Scott
|> |> Peter XXXIII I/III) writes:
|> |> |> a += b =>  a = a + b
|> |> |> a + b =>  tmp = a; a + b; tmp
|> |> |> Similarly for other composite operators.
|> |> |>
|> |> |> a != b => !(a == b) and vice versa
|> |> |> a >= b => !(a < b) etc
|> |> |> a < b => a - b < 0 etc
|> |> |> a - b => a + (-b) and vice versa
|> |> |> -b  => 0 - b
|> |> |> +b  => b
|> |> |> a++  => a += 1 also --
|> |> |> a->b  =>  (*a).b
|> |>
|> |> The standard header <utility> (20.2.1) already defines template-
|> |> operators for the relational operators, deducing them to == and <
|> |> a != b  => !(a == b)
|> |> a >  b  =>   b < a
|> |> a <= b  => !(b < a)
|> |> a >= b  => !(a < b)
|> |>
|> |> You can extend this idea:
|> |>
|> |> template<class T> T& operator+=(T&a, T&b) { return a = a + b; }
|> |> template<class T> T  operator- (T&a, T&b) { return a + (-b); }
|> |> template<class T> T  operator- (T&a)      { return 0 - a; }
|> |> template<class T> T& operator+ (T&a)      { return a; }
|> |> template<class T> T& operator++(T&a)      { return a += 1; }
|> |> template<class T> T& operator++(T&a,int)  { return ++a; }
|>
|> This will not work: it prevent you from writing any templates
|> for these operators over types that do not have "1" or "0"
|> or that need a definition other than the one above.
|>
|> --
|> John.
|>
|> John J. Barton        jjb@watson.ibm.com            (914)784-6645
|>  <http://www.research.ibm.com/xw-SoftwareTechnology>
|> H1-C13 IBM Watson Research Center P.O. Box 704 Hawthorne NY 10598
|> ---
|> [ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
|>   Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
|>   is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]

The substitution for a+=b also won't work; if evaluation of a
has a side effect the side effect will happen twice.

--
rp

---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]





Author: John Max Skaller <maxtal@suphys.physics.su.oz.au>
Date: 1995/11/08
Raw View
jjb@watson.ibm.com (John Barton) wrote:
>In article <47dn87$a17@sunsystem5.informatik.tu-muenchen.de>,
>|> You can extend this idea:
>|>
>|> template<class T> T  operator- (T&a, T&b) { return a + (-b); }
>|> template<class T> T  operator- (T&a)      { return 0 - a; }
>|> template<class T> T& operator+ (T&a)      { return a; }
>|> template<class T> T& operator++(T&a)      { return a += 1; }
>|> template<class T> T& operator++(T&a,int)  { return ++a; }
>
>This will not work: it prevent you from writing any templates
>for these operators over types that do not have "1" or "0"
>or that need a definition other than the one above.

How does it prevent it? [Quote WP ??]


--
John Max Skaller               voice: 61-2-566-2189
81 Glebe Point Rd              fax:   61-2-660-0850
GLEBE NSW 2037                 email: maxtal@suphys.physics.oz.au
AUSTRALIA                      email: skaller@maxtal.com.au
---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]





Author: vandevod@cs.rpi.edu (David Vandevoorde)
Date: 1995/11/11
Raw View
>>>>> "JB" == John Barton <jjb@watson.ibm.com> writes:
JB> In article <47qnbu$f48@oznet03.ozemail.com.au>, John Max Skaller <maxtal@su
phys.physics.su.oz.au> writes:
JB> |> jjb@watson.ibm.com (John Barton) wrote:
JB> |> >In article <47dn87$a17@sunsystem5.informatik.tu-muenchen.de>,
JB> |> >|> You can extend this idea:
JB> |> >|>
JB> |> >|> template<class T> T  operator- (T&a, T&b) { return a + (-b); }
JB> |> >|> template<class T> T  operator- (T&a)      { return 0 - a; }
JB> |> >|> template<class T> T& operator+ (T&a)      { return a; }
JB> |> >|> template<class T> T& operator++(T&a)      { return a += 1; }
JB> |> >|> template<class T> T& operator++(T&a,int)  { return ++a; }
JB> |> >
JB> |> >This will not work: it prevent you from writing any templates
JB> |> >for these operators over types that do not have "1" or "0"
JB> |> >or that need a definition other than the one above.
JB> |>
JB> |> How does it prevent it? [Quote WP ??]

JB> template<class T>
JB> Matrix<T> operator-(Matrix<T>& rhs) { /* doesn't matter what I code here */
 }

JB> The global operator-(T&) will always win over any more
JB> specific template like the one above.  Unless the standard has
JB> changed here as well.

AFAIK, this has never been true. My understanding is that now the
``most specialized'' (if determinable) specialization will be
selected. Before that, I think you would have an ambiguity.

A template `t<U, V, ...>' would be ``more specialized'' than a template
`t<X, Y, ...>' if there exists some subsitution for `X', `Y', ... that
makes `t<X, Y>' identical to `t<U, V>' but not vice versa.

 Daveed
---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]





Author: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
Date: 1995/11/11
Raw View

rp@iscp.bellcore.com (Robert Pearlman) writes:

>jjb@watson.ibm.com (John Barton) writes:
>|> schuenem@informatik.tu-muenchen.de (Ulf Schuenemann) writes:
>|> |> template<class T> T& operator+=(T&a, T&b) { return a = a + b; }
>|> |> template<class T> T  operator- (T&a, T&b) { return a + (-b); }
>|> |> template<class T> T  operator- (T&a)      { return 0 - a; }
>|> |> template<class T> T& operator+ (T&a)      { return a; }
>|> |> template<class T> T& operator++(T&a)      { return a += 1; }
>|> |> template<class T> T& operator++(T&a,int)  { return ++a; }
>|>
>|> This will not work: it prevent you from writing any templates
>|> for these operators over types that do not have "1" or "0"
>|> or that need a definition other than the one above.

I don't think that is correct now that partial specialization is
allowed - if you need a different definition, can't you just define a
(template) specialization?

>The substitution for a+=b also won't work; if evaluation of a
>has a side effect the side effect will happen twice.

That's definitely not correct.  Templates are not just macro substitution.
The arguments are only evaluated once.

--
Fergus Henderson              WWW: http://www.cs.mu.oz.au/~fjh
fjh@cs.mu.oz.au               PGP: finger fjh@128.250.37.3


---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]