C++ 增压装置-使用无量纲类型的任意系统
我正在尝试制作一个有助推单位的维度向量类C++ 增压装置-使用无量纲类型的任意系统,c++,boost,metaprogramming,units-of-measurement,C++,Boost,Metaprogramming,Units Of Measurement,我正在尝试制作一个有助推单位的维度向量类 //vector will be constructed vec<si::length> v(10, 1.0*si::metre); template<typename dimension> class vec { public: //constructor setting all values to q. vec(const size_t, const boost::units::quantity<dimens
//vector will be constructed vec<si::length> v(10, 1.0*si::metre);
template<typename dimension>
class vec
{
public:
//constructor setting all values to q.
vec(const size_t, const boost::units::quantity<dimension> q)
//etc
}
或者是某种元编程的魔力(我注意到boost::units::is_无量纲存在,但我不知道如何使用它,因为我不精通一般的元编程技术)
模板
模板
vec&
向量::运算符*=(常量向量和b){
//进行编译时检查以确保无量纲类型实际上是无量纲的?
//函数的其余部分
}
我想编译以下示例
vec<si::dimensionless> d(10, 2.0);
vec<si::length> l(10, 2.0*si::metre);
l*=d;
vec<cgs::dimensionless> d2(10, 2.0);
vec<cgs::length> l2(10, 2.0*cgs::centimetre);
l2*=d2;
vecd10,2.0;
vec l(10,2.0*si::米);
l*=d;
vecd2(10,2.0);
向量l2(10,2.0*cgs::厘米);
l2*=d2;
我可能会误解Boost的细节,但是按照惯例,double
是无量纲的类型。好的,在检查了库的细节(并了解了Boost\u MPL\u ASSERT)之后,结果很简单。向图书馆设计师致意
template<typename a_dimensionless_type>
vec<dimension>&
operator*=(const vec< a_dimensionless_type >& b)
{
BOOST_MPL_ASSERT(( boost::units::is_dimensionless<boost::units::quantity<a_dimensionless_type> > ));
//the rest of the function
};
模板
vec&
运算符*=(常量向量&b)
{
BOOST_MPL_断言((BOOST::units::is_无量纲));
//函数的其余部分
};
任何给定系统的无量纲类型(例如si::dimensionless)都有一个到double的隐式转换,但是,在上面的示例中,我无法定义vec,因为您不能定义boost::units::quantity,只有boost::units::quantity(例如),这可能需要\include
vec<si::dimensionless> d(10, 2.0);
vec<si::length> l(10, 2.0*si::metre);
l*=d;
vec<cgs::dimensionless> d2(10, 2.0);
vec<cgs::length> l2(10, 2.0*cgs::centimetre);
l2*=d2;
template<typename a_dimensionless_type>
vec<dimension>&
operator*=(const vec< a_dimensionless_type >& b)
{
BOOST_MPL_ASSERT(( boost::units::is_dimensionless<boost::units::quantity<a_dimensionless_type> > ));
//the rest of the function
};