|
Boost : |
From: Victor A. Wagner Jr. (vawjr_at_[hidden])
Date: 2004-09-25 05:16:06
At Saturday 2004-09-18 03:44, Terje Slettebø wrote:
[deleted]
>Even for + and -, there are differences, such as that you can subtract two
>pointers, but not add them.
The erroneous assumption that this is true is something that has bothered
me for some time (and indeed I have worked on systems where it simply isn't
true, that the result of a+b would be useful even if both were
pointers). In an abstract sense it's not true on any system.
Consider:
T* a;
T* b;
.... get them assigned meaningfully to something in a sequence "container"
The midpoint between them is (this is legal using your constraints):
a + (b-a)/2
1st year algebra teaches us that this is equivalent to:
(a+b)/2
While on most systems the intermediate value (a+b) would be useless, as
soon as you divide by 2 it again becomes useful.
with a third pointer (T* c;):
(a+b+c)/3
becomes calculable and useful though clearly
(a+b+c)/2 is NOT useful.
Due to the architecture of the machine we were building, we were forced to
examine this rather carefully and, of course, our assembler handled
expressions of this nature. We used a system somewhat like the one
described by Scott Meyers in his forward to "Modern C++ Design" where he
references a 1995 C++ Report article by John Barton and Lee Nackman which
posits a template solution to perform typesafe dimensional analysis with
zero runtime cost. We had only two dimensions to worry about (memoryness
and relocatableness), but it was up and running in the mid '70s.
I don't mean to pick on you in particular. I just got tired of hearing
people repeat the "mantra" of "you can subtract two pointers, but not add
them". It's simply NOT true.
It has also been somewhat of a disappointment that the HLL folks (and C in
particular (you can program right down to the sand) have managed to ignore
this)
I'll also note, in passing, that for integers the two are equivalent also,
but very few compilers will generate code to guarantee the correct result
if you attempt to calculate (a+b)/2 which _does_ exist on every computer
out there.
[deleted]
Victor A. Wagner Jr. http://rudbek.com
The five most dangerous words in the English language:
"There oughta be a law"
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk