Boost logo

Boost :

From: eford_at_[hidden]
Date: 2001-09-15 02:09:33


> construct each object, but for other software you would want to
> associate specific units to the objects and to have
>
> time_in_seconds = distance_in_cm / velocity_in_mph
>
> to give a compiler error. Most of my work falls in the latter
> category. That's why I'm asking for the more basic library
> that does not have automatic conversions between units that
> measure the same quantity.

How would this differ from?

template<class T, int ID>
class unit_unconvertible :
boost::totally_ordered<unit_unconvertible<T,ID> >,
boost::integer_arithmetic<unit_unconvertible<T,ID> >,
boost::unit_steppable<unit_unconvertible<T,ID> >
  {
  public:
    typedef unit_unconvertible<T,ID> unit_unconvertible_type;
    typedef unit_unconvertible_type self_type;
    typedef self_type& self_reference;
    typedef const self_reference self_const_reference;
    typedef self_const_reference self_param_type;
    typedef typename boost::call_traits<T>::value_type value_type;
    typedef typename boost::call_traits<T>::reference reference;
    typedef typename boost::call_traits<T>::param_type param_type;
    typedef int id_type;
  protected:
    static const id_type mId = ID;
    value_type mVar;
    reference get() { return mVar; };
  public:
    unit_unconvertible(param_type x) : mVar(x) {};
    param_type get() const { return mVar; };

     bool operator<(self_param_type x) const { return (get()<x.get());
};
     bool operator==(self_param_type x) const { return
(get()==x.get()); };
     self_reference operator+=(self_param_type x) { get()+=x.get();
return *this; };
     self_reference operator-=(self_param_type x) { get()-=x.get();
return *this; };
     self_reference operator*=(self_param_type x) { get()*=x.get();
return *this; };
     self_reference operator/=(self_param_type x) { get()/=x.get();
return *this; };
     self_reference operator%=(self_param_type x) { get()%=x.get();
return *this; };
     self_reference operator++() { get()++; return *this; };
     self_reference operator--() { get()--; return *this; };
     
     };

template<class T, int ID>
  ostream& operator<<( ostream& os, const unit_unconvertible<T,ID>& x)
{ return (os << x.get()); };

int main(int argc, char** argv)
{
  unit_unconvertible<float,1> x = 1.;
  unit_unconvertible<float,1> y = 2.;
  cout << "x = " << x << " y = " << y << endl;
  x+=y;
  y*=x;
  cout << "x = " << x << " y = " << y << endl;
  return 0;
}


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk