Article View: comp.std.c++
Article #2454Re: Implicit operation relationships
From: Robert Pearlman
Date: Tue, 07 Nov 1995 18:23
Date: Tue, 07 Nov 1995 18:23
60 lines
2952 bytes
2952 bytes
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. ]
Technical Details
Message-ID:
<47o4oo$6ep@athos.cc.bellcore.com>Path:
arc1-ca.pugleaf.net!128.32.155.1!mattReferences:
<MATT.95Aug24221328@Austern> <46vaga$dng@marina.cinenet.net> <47dn87$a17@sunsystem5.informatik.tu-muenchen.de> <47l76s$qu1@watnews2.watson.ibm.com>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!128.32.155.1!matt From: rp@iscp.bellcore.com (Robert Pearlman) Newsgroups: comp.std.c++ Subject: Re: Implicit operation relationships Date: 07 Nov 1995 18:23:36 GMT Organization: Bellcore Approved: matt@physics2.Berkeley.EDU (Matt Austern) Message-ID: <47o4oo$6ep@athos.cc.bellcore.com> References: <MATT.95Aug24221328@Austern> <46vaga$dng@marina.cinenet.net> <47dn87$a17@sunsystem5.informatik.tu-muenchen.de> <47l76s$qu1@watnews2.watson.ibm.com> NNTP-Posting-Host: physics2.berkeley.edu