Article View: comp.std.c++
Article #2422Re: Implicit operation relationships
From: Ulf Schuenemann
Date: Fri, 03 Nov 1995 19:21
Date: Fri, 03 Nov 1995 19:21
46 lines
2295 bytes
2295 bytes
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; }
But unfortunately you can't do it this way for -> as it must be
a nonstatic memberfct (13.5.6 [over.ref]).
Ulf Schuenemann
--------------------------------------------------------------------
,_. Ulf Schuenemann
#, \ Fakultaet fuer Informatik, Technische Universitaet Muenchen, Germany.
| > email: schuenem@informatik.tu-muenchen.de
v=-< WWW: http://hphalle2.informatik.tu-muenchen.de/~schuenem/
---
[ 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:
<47dn87$a17@sunsystem5.informatik.tu-muenchen.de>Path:
arc1-ca.pugleaf.net!in1.uu.net!bounce-backReferences:
<MATT.95Aug24221328@Austern> <46vaga$dng@marina.cinenet.net>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!in1.uu.net!bounce-back Date: 03 Nov 95 19:21:17 GMT Approved: fjh@cs.mu.oz.au From: schuenem@informatik.tu-muenchen.de (Ulf Schuenemann) Newsgroups: comp.std.c++ Subject: Re: Implicit operation relationships X-Original-Date: 3 Nov 1995 18:32:39 GMT Organization: Technische Universitaet Muenchen, Germany Message-ID: <47dn87$a17@sunsystem5.informatik.tu-muenchen.de> References: <MATT.95Aug24221328@Austern> <46vaga$dng@marina.cinenet.net> X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBFAgUBMJpr0uEDnX0m9pzZAQE2WgF7BKoy+IpJk/xv/ODEnwTd/BmiIzkCBhnn uF5fzntHuCIbh8V+MVbezSnn5tNmyDsz =JHAt