Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boost-units - using a dimensionless type of arbitrary system

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;
like image 640
Tom Avatar asked Feb 18 '11 11:02

Tom


1 Answers

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
 };
like image 83
Tom Avatar answered Oct 11 '22 16:10

Tom