开发者

boost-units - using a dimensionless type of arbitrary system

开发者 https://www.devze.com 2023-02-12 02:07 出处:网络
I am trying to make a dimensioned vector class with boost-units like so, //vector will be constructed vec<si::length> v(10, 1.0*si::metre);

I am trying to make a dimensioned vector class with boost-units like so,

//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
}

It all works fine except for the operator*= and operator/= that do element wise multiplication and division. Since these do not change the dimension, they only make sense when multiplying/dividing by a dimensionless quantity: I am struggling to find an arbitrary dimensionless quantity that is not locked into a specific system (e.g. si or cgs units).

I want something like,

  开发者_运维知识库/** Multiply a dimensionless vector. */
  vec<dimension>& 
  operator*=(const vec<boost::units::dimensionless_type>& b);

or perhaps some metaprogramming magic (I notice boost::units::is_dimensionless exists, but I have no idea how to use it as I am not versed in general metaprogramming techniques)

  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
  }

I want following examples to compile

  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;


Okay, after examining the library details (and learning about BOOST_MPL_ASSERT) it turned out to be very easy. My compliments to the library designer.

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
 };


I might be mistaken about Boost details, but conventionally double is the dimensionless type.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号