boost-units - 使用无量纲类型的任意系统

Tom*_*Tom 6 c++ boost metaprogramming units-of-measurement

我试图用这样的boost-units制作一个带尺寸的矢量类,

//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
}
Run Code Online (Sandbox Code Playgroud)

这一切工作正常,除了operator*=operator/=那些元素方式乘法和除法.由于这些不会改变维度,因此只有在乘以/除以无量纲数量时才有意义:我正在努力寻找未锁定到特定系统的任意无量纲数量(例如si或cgs单位).

我想要的东西,

  /** Multiply a dimensionless vector. */
  vec<dimension>& 
  operator*=(const vec<boost::units::dimensionless_type>& b);
Run Code Online (Sandbox Code Playgroud)

或许是一些元编程魔术(我注意到boost :: units :: is_dimensionless存在,但我不知道如何使用它,因为我不熟悉一般的元编程技术)

  template<typename dimension>
  template<typename a_dimensionless_type>
  vec<dimension>& 
  vec<dimension>::operator*=(const vec<a_dimensionless_type>& b){
    //some compile time check to make sure that a_dimensionless_type is actually dimensionless?
    //the rest of the function
  }
Run Code Online (Sandbox Code Playgroud)

我想要以下示例进行编译

  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;
Run Code Online (Sandbox Code Playgroud)

Tom*_*Tom 4

好的,在检查了库详细信息(并了解了 BOOST_MPL_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
 };
Run Code Online (Sandbox Code Playgroud)