Boost logo

Boost :

From: helmut.zeisel_at_[hidden]
Date: 2001-08-30 06:57:52

--- In boost_at_y..., David Abrahams <david.abrahams_at_r...> wrote:
> Some questions about the new text:

> > These signatures, however, are provided for
> > symmetry reasons and to prevent potential conflicts with other
equally well
> > matching operator signatures.

I put more emphasis on these "operator signatures"
since the are actually more important than performance.
I hope that the given example is convincing.

An updated version can be found at,

below I include the main change in the text.


--- start changed text

Mixed Arithmetics

Another application of the two-argument template forms is for mixed
arithmetics of a type T and a type U convertible to T. In that case
there are two points where
the two-argument template forms are helpful: One is to provide the
respective signatures for operator overlading, the second is

With respect to the operator overloading assume e.g. that U is int,
that T is a user defined unlimited integer type, and that double
operator-(double, const
T&) exists. If one wants to compute int - T and does not provide T
operator-(int, const T&), the compiler will consider double
const T&) to be a better match than T operator-(const T&, const T&),
which will probably be different from the user's intention. To define
a complete set
of operator signatures, additional 'left' forms of the two-argument
template forms are provided (subtractable2_left<T,U>,
modable2_left<T,U>) that define the signatures for non-commutative
operators where U appears on the left hand side (operator-(const U&,
const T&),
operator/(const U&, const T&), operator%(const U&, const T&)).

With respect to the performance observe that when one uses the single
type binary operator for mixed type arithmetics, the type U argument
has to be converted to
type T. In practice, however, there are often more efficient
implementations of, say T::operator-=(const U&) that avoid
conversions from U to T.
The two-argument template forms of the arithmetic operator create
additional operator interfaces that use these more efficient
implementations. There is, however,
no performance gain in the 'left' forms: They still need a conversion
from U to T and have an implementation equivalent to the code that
would be automatically
created by the compiler if it considered the single type binary
operator to be the best match.

--- end changed text

Boost list run by bdawes at, gregod at, cpdaniel at, john at