From: Andy Little (andy_at_[hidden])
Date: 2004-01-04 10:57:42
"Deane Yang" <deane_yang_at_[hidden]> wrote in message
> Andy Little wrote:
> > explicit if overused is just another implicit
> > static_cast, dynamic_cast etc provide a better more diverse precursor
> > conversions.
> > name over structure seems to work better in template coding... it can
> > provide a more explicit contract
> Any chance you could elaborate on this? I apologize if I'm wrong,
> but I think you might have written this while you were bouncing
> a baby on your lap.
Well caught ... not a baby...was just drunk.... apologies
I am finding the Invariants library discussion very interesting... I'm
probably using 'contract' incorrectly... so ignore that.
the template coding line also makes no sense... so ignore that too.
Maybe using 'structural' incorrectly... bear that in mind .. in hope you
can make some sense of my useage of 'structural' in the following
On structure v name. explicit is a 'structural' thing. I am already using
that for value ctors
In that context it represents a conversion from a numeric to a q_length::m .
It is a structural conversion.
There are many precedents for this type of use, so it feels natural
notably it would be perfectly 'safe' to do the following... it is merely an
initialisation of the value_type:
q_length::m LL = 1; // is an error in reality
but doing that violates dimensional analysis. therefore I must use
'explicit'(or something) here though not for any safety but for structural
L1 = L0;
Conceptually I am perfectly happy with this. L0 and L1 are the same length,
but expressed in different units.
Therefore is not a structural conversion. It does not violate dimensional
However it is not safe... there will be a change in the actual value which
might cause overflow.
And it is not immediately clear that the danger is there.
L1 = q_length::mm(L0);
does not feel right... its implies a structural conversion... but the
conversion is no big deal to me conceptually,
So I am contemplating an alternative:
L1 = unit_cast<q_length::mm>(L0);
It is telling you exactly what it is doing, its not pretending to be a
structural conversion but it is explicit.
AND the following wont work:
L1 = unit_cast<q_length::mm>(1); // error
which I feel is an improvement because the only correct argument is
unequivocably a q_length::xxx;
whereas If I used 'explicit' again in this context either a length or a
numeric would be acceptable argument.
> I find myself automatically making any eligible constructor explicit
> and removing it later only in a few rare circumstances. But I also
> almost never use default arguments anymore, either. I just like
> everything to be staring me in the face, when I'm trying to fix my
> code or just trying to remember what code I wrote more than week ago
> does. And the compiler warns me if I accidentally do a cast that I
> didn't mean to.
> I also wish it were possible to turn off all those automatic conversions
> like from int to double, too. It definitely causes me more grief
> than gain. Unfortunately, you can't even turn on the warnings about
> these conversions, because you get a gazillion warnings from the
> C and C++ library code.
Yuk...Which library is this ?
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk