Boost logo

Boost Users :

Subject: Re: [Boost-users] [units] (controlled) implicit conversion
From: Matthias Schabel (boost_at_[hidden])
Date: 2010-04-30 12:17:00


> I took much labour in defining an extra system of units (atomic
> units). It works very well and it also defines (optionaly) default
> conversion from atomic to SI units. The problem that I have now is
> that all the conversion has to be done explicitly. I understand that
> this way of doing thing is by design.

Perhaps you would be willing to add the atomic system to the Boost distribution?
I expect that there is a reasonable subset of users for whom this would add
value...

> But for some formulas I would like to allow implicit conversion, for
> example when adding energies in SI and energies in atomic units (I
> don't care which direction the conversion is done since I will
> transform to one of the two at the end of the formula). On top of that
> the Units Manual says:
>
> "Safety and the potential for unintended conversions leading to
> precision loss and hidden performance costs. Options are provided for
> forcing implicit conversions between specific units to be allowed. "
>
> The question is which are the "Options provided for forcing implicit
> conversion"?

My recollection is that, after much debate, discussion, and contemplation, it was
decided that allowing implicit conversions created too many problems and
ambiguities, some of them subtle. That being said, it is a common request for functions
that take arbitrary units of specified dimension. This is relatively straightforward to accomplish
(see these threads for more examples : http://lists.boost.org/boost-users/2009/03/45745.php
and http://lists.boost.org/boost-users/2010/04/58340.php) :

template<class Y,class System>
void f(const quantity<unit<desired_dimension_type,System>,Y>& arg)
{
        const quantity<desired_unit_to_use_for_internal_calculations,Y> q(arg);

        // do something with q
        return;
}

This doesn't solve your specific request for implicit assignment to work, but is the recommended
way of doing "implicit" conversion in function arguments. Hope this helps.

Matthias

>
> What follow is an example:
>
> What I want is some controlled implicit conversion (even better is
> this conversion are allowed inside a certain program scope). Note that
> line 4 doesn't work:
>
> quantity<si::energy> e1(3.*si::joules);
> quantity<atomic::energy> e2(4.*atomic::hartree);
> quantity<atomic::energy> e3(e1); //works
> e1 = e2; //doesn't work
> e1 = quantity<si::energy>(e2); //works
> cout<<e1<<'\n'<<e2<<'\n'<<e3<<endl;
>
> note that the third line works because somewhere I defined a
> conversion_factor plus a default_conversion between units in the
> different systems. And it works because the conversion is explicit.
> Now the fourth line doesn't work, it gives an error (see at end).
> comenting the third line compiles.
>



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net