0

I am trying to use Boost.Units in my project. I mostly have it working, but I would like to have an accessor method that will allow me to get the raw value using different scaling values. For example, I would like to get a value in volts one time, but at another point I need the value in millivolts. I am able to convert the numbers using the conversion_factor method. However, I would like to make it more convenient to use with an accessor that has a default scale passed in. Here is the accessor method for my class I am trying to use:

virtual double get_vm(electric_potential scale = volts) { return (quantity_cast<double>(m_vm)* conversion_factor(volts, scale)); };

I am attempting to use it like this:

static const auto millivolts = milli * volts;
double dblVal = get_vm(millivolts);

However, this will not compile. I get the following error:

1>IF_base_cell_type.cpp(31): error C2664: 'double IF_base_cell_type::get_v_rest(boost::units::si::electric_potential)' : cannot convert argument 1 from 'const boost::units::unit<boost::units::list<boost::units::dim<DT1,boost::units::static_rational<2,1>>,boost::units::list<boost::units::dim<Derived,boost::units::static_rational<1,1>>,boost::units::list<boost::units::dim<DT3,boost::units::static_rational<-3,1>>,boost::units::list<boost::units::dim<DT4,boost::units::static_rational<-1,1>>,boost::units::dimensionless_type>>>>,boost::units::heterogeneous_system<boost::units::heterogeneous_system_impl<boost::units::list<T,L>,boost::units::list<boost::units::dim<DT1,boost::units::static_rational<2,1>>,boost::units::list<boost::units::dim<Derived,boost::units::static_rational<1,1>>,boost::units::list<boost::units::dim<DT3,boost::units::static_rational<-3,1>>,boost::units::list<boost::units::dim<DT4,boost::units::static_rational<-1,1>>,boost::units::dimensionless_type>>>>,boost::units::list<boost::units::scale_list_dim<Scale>,boost::units::dimensionless_type>>>,void>' to 'boost::units::si::electric_potential'
1>          with
1>          [
1>              DT1=boost::units::length_base_dimension
1>  ,            Derived=boost::units::mass_base_dimension
1>  ,            DT3=boost::units::time_base_dimension
1>  ,            DT4=boost::units::current_base_dimension
1>  ,            T=boost::units::heterogeneous_system_dim<boost::units::si::meter_base_unit,boost::units::static_rational<2,1>>
1>  ,            L=boost::units::list<boost::units::heterogeneous_system_dim<boost::units::scaled_base_unit<boost::units::cgs::gram_base_unit,boost::units::scale<10,boost::units::static_rational<3,1>>>,boost::units::static_rational<1,1>>,boost::units::list<boost::units::heterogeneous_system_dim<boost::units::si::second_base_unit,boost::units::static_rational<-3,1>>,boost::units::list<boost::units::heterogeneous_system_dim<boost::units::si::ampere_base_unit,boost::units::static_rational<-1,1>>,boost::units::dimensionless_type>>>
1>  ,            Scale=boost::units::scale<10,boost::units::static_rational<-3,1>>
1>          ]
1>          No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

I am obviously doing something wrong, but the error that is reported is so difficult to parse with all the template code in it that I am not sure what I am doing wrong. I was hoping someone may have done something similar and would be able to tell me where I am have gone astray?

dcofer
  • 303
  • 2
  • 10
  • Look at `make_scaled_unit` instead of `milli * volts`, at [this doc page](http://www.boost.org/doc/libs/1_57_0/doc/html/boost_units/Units.html#boost_units.Units.scaled_base_units) – Mike C Apr 08 '15 at 17:56

1 Answers1

0

A bit late, but maybe it can help somebody looking for the same answer...

In Boost.Units, units are implemented as types. It is important to note that the type of the following two expressions is different:

auto centiimeters = centi * meter;
auto millimeters = milli * meter;

Therefore, the only way to use the above as an argument of a function, is to make it a template:

template <typename T>
double get_vm (T unit) {
    return (quantity_cast<double>(m_vm)* conversion_factor(volts, unit)); 
}

Use as:

double my_vm = get_vm(milli * meter);
matpen
  • 281
  • 2
  • 15