C++ 使用Boost.Units定义百分比

C++ 使用Boost.Units定义百分比,c++,boost,units-of-measurement,C++,Boost,Units Of Measurement,我想用Boost.Units实现一个percent单位,这样一个无量纲的数量(比如比率)就可以表示为一个百分比。我已经成功地实现了质量密度单位之间的转换,但是对于无量纲单位,这种转换不起作用。这是我的代码(假设使用名称空间boost::units;): 但以下两种转换都无法编译: quantity<percent_unit> q2my(3*percent); quantity<si::dimensionless> q2si(q2my); quantity<perce

我想用Boost.Units实现一个
percent
单位,这样一个无量纲的数量(比如比率)就可以表示为一个百分比。我已经成功地实现了质量密度单位之间的转换,但是对于无量纲单位,这种转换不起作用。这是我的代码(假设
使用名称空间boost::units;
):

但以下两种转换都无法编译:

quantity<percent_unit> q2my(3*percent);
quantity<si::dimensionless> q2si(q2my);
quantity<percent_unit> q2back(q2si);
//
// percentage (%)
//
namespace my {
    struct percent_base_dimension : 
        base_dimension<percent_base_dimension, 1> {};
    typedef percent_base_dimension::dimension_type percent_type;

    struct percent_base_unit :
        base_unit<percent_base_unit, percent_type, 1>
    {
        static std::string name() {return "percent";}
        static std::string symbol() {return "%";}
    };
    typedef make_system<percent_base_unit>::type system;
    typedef percent_base_unit::unit_type percent_unit;
    BOOST_UNITS_STATIC_CONSTANT(percent, percent_unit);
}

namespace boost { namespace units {

template<class T0, class T1>
struct conversion_helper<quantity<my::percent_unit, T0>, quantity<si::dimensionless, T1> >
{
    static quantity<si::dimensionless, T1> convert(const quantity<my::percent_unit, T0>& source)
    {
        return(quantity<si::dimensionless, T1>::from_value(1e-2 * source.value()));
    }
};

template<class T0, class T1>
struct conversion_helper<quantity<si::dimensionless, T0>, quantity<my::percent_unit, T1> >
{
    static quantity<my::percent_unit, T1> convert(const quantity<si::dimensionless, T0>& source)
    {
        return(quantity<my::percent_unit, T1>::from_value(1e+2 * source.value()));
    }
};

} }

int main()
{
    using namespace my;

    quantity<percent_unit> q2my(3*percent);

    //quantity<si::dimensionless> q2si(q2my);
    //The converter won't be picked up due to an explicit disable_if in quantity.hpp:
    // typename boost::disable_if<detail::is_dimensionless_system<System2> >::type* = 0
    //so we manually force the conversion here:
    auto conv = conversion_helper<quantity<percent_unit>, quantity<si::dimensionless> >::convert;
    quantity<si::dimensionless> q2si(conv(q2my));

    quantity<percent_unit> q2back(q2si);

    std::cout 
        << "q2my: " << q2my << std::endl
        << "q2si: " << q2si << std::endl
        << "q2back: " << q2back << std::endl
        ;
}
<代码>第二季度车型年款的数量(百分之三); 数量q2si(q2my); 数量q2back(q2si); G++输出:
调用“conversion\u factor(…,…)”时没有匹配的函数。

这是否与
无量纲类型
似乎是类型列表末尾的标记有关

如有任何帮助或建议,将不胜感激。多谢各位

这是否与无量纲类型似乎是类型列表末尾的标记这一事实有关

有点<代码>无量纲单位类型隐式包含在每个测量系统中,并以相同的方式从中提取,请参见
boost/units/dimensional\u units.hpp

在您的“百分比”示例中,思考您的新测量系统将是什么,以及您将如何根据常用的增压单位规则指定它:

namespace my {
    ... // define your own unit tag types
    typedef make_system</* your system's units with dimensions*/>::type system;
    ... // unit typedefs, consts, etc.
}
名称空间我的{
…//定义您自己的单元标记类型

typedef make_system多好的回答!谢谢。
namespace my {
    ... // define your own unit tag types
    typedef make_system</* your system's units with dimensions*/>::type system;
    ... // unit typedefs, consts, etc.
}
//
// percentage (%)
//
namespace my {
    struct percent_base_dimension : 
        base_dimension<percent_base_dimension, 1> {};
    typedef percent_base_dimension::dimension_type percent_type;

    struct percent_base_unit :
        base_unit<percent_base_unit, percent_type, 1>
    {
        static std::string name() {return "percent";}
        static std::string symbol() {return "%";}
    };
    typedef make_system<percent_base_unit>::type system;
    typedef percent_base_unit::unit_type percent_unit;
    BOOST_UNITS_STATIC_CONSTANT(percent, percent_unit);
}

namespace boost { namespace units {

template<class T0, class T1>
struct conversion_helper<quantity<my::percent_unit, T0>, quantity<si::dimensionless, T1> >
{
    static quantity<si::dimensionless, T1> convert(const quantity<my::percent_unit, T0>& source)
    {
        return(quantity<si::dimensionless, T1>::from_value(1e-2 * source.value()));
    }
};

template<class T0, class T1>
struct conversion_helper<quantity<si::dimensionless, T0>, quantity<my::percent_unit, T1> >
{
    static quantity<my::percent_unit, T1> convert(const quantity<si::dimensionless, T0>& source)
    {
        return(quantity<my::percent_unit, T1>::from_value(1e+2 * source.value()));
    }
};

} }

int main()
{
    using namespace my;

    quantity<percent_unit> q2my(3*percent);

    //quantity<si::dimensionless> q2si(q2my);
    //The converter won't be picked up due to an explicit disable_if in quantity.hpp:
    // typename boost::disable_if<detail::is_dimensionless_system<System2> >::type* = 0
    //so we manually force the conversion here:
    auto conv = conversion_helper<quantity<percent_unit>, quantity<si::dimensionless> >::convert;
    quantity<si::dimensionless> q2si(conv(q2my));

    quantity<percent_unit> q2back(q2si);

    std::cout 
        << "q2my: " << q2my << std::endl
        << "q2si: " << q2si << std::endl
        << "q2back: " << q2back << std::endl
        ;
}
namespace my {
    template<class Y>
    quantity<si::dimensionless, Y> units(const quantity<percent_unit, Y>& source)
    {
        return(quantity<si::dimensionless, Y>::from_value(1e-2 * source.value()));
    }

    template<class Y>
    quantity<percent_unit, Y> percentage(const quantity<si::dimensionless, Y>& source)
    {
        return(quantity<percent_unit, Y>::from_value(1e+2 * source.value()));
    }

}

int main()
{
    using namespace my;

    quantity<percent_unit> q2my(3*percent);
    quantity<si::dimensionless> q2si(my::units(q2my));
    quantity<percent_unit> q2back(my::percentage(q2si));

    std::cout 
        << "q2my: " << q2my << std::endl
        << "q2si: " << q2si << std::endl
        << "q2back: " << q2back << std::endl
        ;
}
template<class Y>
quantity<si::dimensionless, Y> units(const quantity<percent_unit, Y>& source)
{
    return quantity<si::dimensionless, Y>(1e-2 * source / percent);
}

template<class Y>
quantity<percent_unit, Y> percentage(const quantity<si::dimensionless, Y>& source)
{
    return quantity<percent_unit, Y>(1e+2 * source * percent);
}