|
Boost : |
From: Gennaro Prota (gennaro_prota_at_[hidden])
Date: 2003-06-19 05:10:16
On Wed, 18 Jun 2003 22:27:27 +0100, "Paul A Bristow"
<boost_at_[hidden]> wrote:
>| -----Original Message-----
>| From: boost-bounces_at_[hidden]
>| [mailto:boost-bounces_at_[hidden]] On Behalf Of Gennaro Prota
>| [...]
>|
>| In Daniel's solution static_cast<type>(x) simply "selects"
>| the corresponding specialization of x_value. So
>|
>| static_cast<float>(pi)
>|
>| is fundamentally different from, say,
>|
>| double pi = ...;
>| static_cast<float>(pi)
>
>Perhaps naively, I find this surprising - but I am not a language guru.
>
>Can you/anyone confirm that this is required by the Standard
> - and is in fact what happens when compilers process this?
Yes :-) Note that all "conversions" from a constant<T, F> pass through
template< typename U > operator U() const { return F< U >()(); }
Now, F<U>()() means (ignoring some technical differences irrelevant
here):
F<U> object; // construct an x_value<U> object
return object(); // invoke operator() on it and return
Now, if you use the binary operators, like in:
pi * f;
with f being a float then the selected specialization is float; i.e.
the constant "takes the type" of its adjacent operand (left operand if
it exists, right operand otherwise). This is, of course, different
from what normally happens when you do
double pi = ...;
pi * f; // floating point promotion on f
and is IMHO a caveat. But, still, there are no truncations. Only
selection of the target specialization. If no target specialization
exists, e.g.
static_cast<int>(pi)
you have a compile-time error.
>
>If it walks like a duck and quacks like a duck ...
This walks quite differently. And doesn't quack :-)
Genny.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk