Article View: comp.std.c++
Article #2443Re: Implicit operation relationships
From: John Barton
Date: Tue, 07 Nov 1995 04:36
Date: Tue, 07 Nov 1995 04:36
47 lines
2506 bytes
2506 bytes
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. ]
Technical Details
Message-ID:
<47l76s$qu1@watnews2.watson.ibm.com>Path:
arc1-ca.pugleaf.net!in1.uu.net!bounce-backReferences:
<MATT.95Aug24221328@Austern> <46vaga$dng@marina.cinenet.net> <47dn87$a17@sunsystem5.informatik.tu-muenchen.de>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!in1.uu.net!bounce-back Date: 07 Nov 95 04:36:19 GMT Approved: fjh@cs.mu.oz.au Return-Path: <daemon@migs.UCAR.EDU> From: jjb@watson.ibm.com (John Barton) Newsgroups: comp.std.c++ Subject: Re: Implicit operation relationships X-Original-Date: 6 Nov 1995 14:47:56 GMT Organization: IBM T.J. Watson Research Center Message-ID: <47l76s$qu1@watnews2.watson.ibm.com> References: <MATT.95Aug24221328@Austern> <46vaga$dng@marina.cinenet.net> <47dn87$a17@sunsystem5.informatik.tu-muenchen.de> X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBFAgUBMJ7iYOEDnX0m9pzZAQH7KwF/Thb6r3hDwUjNgfY4pRZAZdOBLUQx1gPV iAvMfeJZ3xba5NTbhPABM1dAS2IIkLX8 =whYz