Boost logo

Boost :

From: Matt Calabrese (rivorus_at_[hidden])
Date: 2005-11-21 18:40:51


On 11/21/05, Andy Little <andy_at_[hidden]> wrote:
>
> It might be as well to note that this functionality is provided by
> Boost.Typeof,
> however Boost.Typeof can cause very slow compile times. (I hope that
> Arkadiy
> Vertleyb and Peder Holt dont take unkindly to that remark!)

Yeah, in the case of a unit library, Boost.Typeof can be used directly with
arithmetic types on an expression using the desired operations with
potentially a bit of an impact on compile times, though the benefit of a
more general metafunction arises when enums are introduced (not in reference
to a units library), since operators you may be using in the typeof
expression may be overloaded, meaning the expression used inside of typeof
would have to result in promotion or usual arithmetic conversions without
using any overloadable operators, which can be a hassle to deal with. I get
around enum operator overloads by just internally using a type extraction
mechanism on the result of a function call whose parameter types and result
types are based off of the set of types restricted by the possible results
of promotion and usual arithmetic conversion as defined by the standard (and
including long long and unsigned long long types if the compiler supports
them as extensions). This way, promotion happens through the passing of
arguments to a function, without any possibility of invoking overloaded
operators. As well, since the possible result types are a small, finite set,
type extraction can be done quickly and easily.

Note though, that these metafunctions are only for arithmetic and enum types
(as that is all that the standard defines promotion and usual arithmetic
conversions for). Typeof on expressions is still the best solution if you
want to provide a way of automatically yielding the result of an arbitrary
expression based off of user-defined types. In my library, forwarded
operations and results are currently provided through policies, defaulting
appropriately for arithmetic types with the aforementioned conversion
metafunctions, and defaulting to the intuitive operators and a metafunction
encapsulating typeof on those operations for user-defined types. This way,
you can get proper automatic results for all arithmetic types without the
need for typeof, automatic results with user-defined types when using typeof
registration and the default policies, or you can specialize the
metafunctions used by the default policies to avoid typeof completely if
necessary.

The names for the metafunctions I'm using are "promote" and
"usual_arithmetic_conversions", since these reflect how they are refered to
in the standard. Your "arithmetic_promote" is probably a better name than my
"usual_arithmetic_conversions" since it's shorter and has a verb, so I'll
make that adjustment. These are really more general purpose than a units
library, so would probably best just be in type_traits in exposed anywhere
at all.

--
-Matt Calabrese

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