Boost logo

Boost :

From: Daryle Walker (darylew_at_[hidden])
Date: 2001-05-17 08:20:13


on 5/16/01 7:51 AM, Aleksey Gurtovoy at alexy_at_[hidden] wrote:

> David Abrahams wrote:
>> Before I look at dlw_oprs.zip I'd like to hear from Aleksey that his
>> concerns have been addressed.
>
> They were, except the 'identity_querible' name, with which I am still
> uncomfortable. Actually, isn't the unary plus operator a part of
> (one-argument) 'addable' concept? IMO, expressions like +a don't make much
> sense if you can't also write a1 + a2 and a1 += a2, and, on the other hand,
> if the latter two are allowed, is there a reason for not providing also the
> +a form? As for me, right now I can't think of one.. so we might have a
> chance to make the library more consistent and get rid of the questionable
> name at the same time :).

I like the name :). We don't need 100% agreement, do we? The unary and
binary versions of operator+ can't be combined, because there could be some
types that don't support both. For instance, strings can use the binary
version for concatenation, but the unary version doesn't make much sense.

> Hmm.., interesting, what about unary operator minus (negation)? Assuming
> that T's default constructor creates an equivalent to zero<T>() (it's not a
> unreasonable assumption, is it?), the following also could make sense
> (although probably not very effective anyway):
>
> friend T operator-(const T& x) { return T() - x; },
>
> and probably not as a member of 'subtractable'. Just a thought :).

I put in the unary operator+ because it's trivial to implement without any
dependencies on the target type (besides copy construction). A unary
operator- needs some dependencies:

1. existence of a binary operator- for the target type
2. existence of a additive identity value for the target type
3. assumption that the target type's default constructor returns that value

It's also a lot more likely that the target type's implementers can provide
a better implemented version of unary operator- than this template. A
native implementation of the unary operator+ can't get simpler than what the
'identity_querible' template has provided.

-- 
Daryle Walker
Mac, Internet, and Video Game Junkie
darylew AT mac DOT com

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