This is the mail archive of the
libstdc++@sourceware.cygnus.com
mailing list for the libstdc++ project.
Re: stl function.h
Steve Hill <stevehi@soc.plym.ac.uk> writes:
> Why not define functors additive_inverse (OK, I know negates does this) and
> multiplicitive_inverse, so the operator() in minus and divides would be
> respectively defined as
>
> T operator()(const T& x,const T& y)
> {
> return plus(x,additive_inverse(y));
> };
>
> and
>
> T operator()(const T& x,const T& y)
> {
> return multiplies(x, multiplicitive_inverse(y));
> };
It's certainly no good idea. Just assume these two situations:
- for the multiplicitive_inverse assume T == float. Now, x = 3 and y = 3.
Any FPU will get the result correct. But if 1/3 is computed first and
is truncated to a `float' the result may differ because you lost quite
some precision.
- for the additive_inverse case. Assume you have T a class defining an
Abel group of some kind. The operations minus, plus, and additive_inverse
all could take a non-constant amount of time. So your proposed definition
will take much longer since two such operations must be performed.
If there are certain classes which could benefit from the inverse
calculation and some matrix classes come into mind, then you should
specialize the operators for these classes. A general definition like
above is not suitable.
--
---------------. drepper at gnu.org ,-. 1325 Chesapeake Terrace
Ulrich Drepper \ ,-------------------' \ Sunnyvale, CA 94089 USA
Cygnus Solutions `--' drepper at cygnus.com `------------------------