Boost logo

Boost Users :

Subject: Re: [Boost-users] [units] unit conversion on construction in relation to argument passing
From: Matthias Schabel (boost_at_[hidden])
Date: 2011-03-25 12:46:35


>> nice! But for some reason I believe this would work for argument
>> functions
>>
>> double f(quantity<si::length> v){ return 1.;}
>> ...
>> quantity<cgs::lengh> A = 4.*cgs::centimeter
>> f(A); // doesn't work! not matching function
>>
>> why is this? isn't the argument of the call a sort of construction
>> argument for the function argument, or is more like a plain
>> assignment. Is there a way to force the automatic conversion of the
>> function call.
>>
>> Or I am forced to use this other long call?
>>
>> f(quantity<si::length>(A));
>
> Yes. The constructor is explicit.

Or you can write it as a template function that takes any argument that is a quantity of length :

#include <iostream>

#include <boost/units/io.hpp>
#include <boost/units/systems/cgs.hpp>
#include <boost/units/systems/si.hpp>

using namespace boost::units;

template<class System,class Y>
Y f(quantity<unit<length_dimension,System>,Y> v)
{
        const Y w = quantity<si::length,Y>(v).value();
        
        std::cout << v << "\t" << w << std::endl;
        
        return w;
}

int main(void)
{
        f(quantity<cgs::length,double>(17.0*cgs::centimeters));
        
        return 0;
}

Matthias



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