Subject: Re: [boost] Numeric constants
From: John Maddock (john_at_[hidden])
Date: 2009-01-31 05:37:22
>How to use those constant ? It rather simple. Things like
>double k = pi_;
>int u = ten_;
>works as intended by auto-casting the valeu to the destination type.
>You can force a constant to be of a given type : abs( float(log2_) );
>Alternatively, the basic operator (+,-,*,/,unary -, ~, ! and comparison)
>are overlaod to autocast the constant into a proper type when used like :
>k = v + pi_;
>bool test = x > log10_;
>A set of result_of enabled traits also allows to computes the correct
>type of a constant when used in a complex expression.
>Most constant type-cast is performed at compile-time, unless if you use
>a user-defined type-casting overload.
>I am overthinking it or could this be viable ?
Well.... this is one of those "complex" solutions that was suggested last
time this was discussed that I've been trying to avoid ;-)
Off the top of my head, the main problems I see are:
* What is the result of 2 * pi? How about 2 * pi * r where r is a real?
* If you mistakenly call a function passing a constant as argument, for
example foo(2.0, pi), then if foo is a template you will likely get an
inscutable failure very deep inside foo's body (or worse some other function
that foo calls).
* In order to avoid ODR violations I presume the constants have to be
defined in an anonymous namespace? Does this cause code bloat simply by
#including the constants header?
Now I suspect that all of these issues are probably solvable to some degree,
it's a question of whether it's worth the hassle, and/or whether the
resulting code is likely to turn into a problem to maintain.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk