Boost logo

Boost :

From: Terje Slettebø (tslettebo_at_[hidden])
Date: 2004-09-26 04:19:22


>From: "Victor A. Wagner Jr."

> At Saturday 2004-09-18 03:44, Terje Slettebø wrote:
>
> >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 this is interesting (and I understand it was not meant to pick on me
in particular, as you said, and it wouldn't really make any sense, as Dave
Abrahams pointed out), my point was strictly about what is legal _C++_, and
the above is not legal C++, which I think you will agree with:

"5.7 Additive operators
[...]
For addition, either both operands shall have arithmetic or enumeration
type, or one operand shall be a
pointer to a completely defined object type and the other shall have
integral or enumeration type."

The context was detecting addability, and if the trait had reported "yes"
for pointers, it would have caused errors elsewhere.

You might also argue that the language should allow the expression above,
but that's a different discussion.

> 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.

With "correct result", I guess you mean the possibility of overflow in this
expression, compared to the other form.

Regards,

Terje


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk