Back to Arangodb

Implementing Addition and Subtraction

3rdParty/boost/1.78.0/libs/mpl/doc/tutorial/implementing-addition-and.html

3.12.9.11.5 KB
Original Source

| Prev Next | Back Along | Up Home | Full TOC | Front Page / Tutorial: Metafunctions and Higher-Order Metaprogramming / Dimensional Analysis / Implementing Addition and Subtraction |

Implementing Addition and Subtraction

We can now easily write the rules for addition and subtraction, since the dimensions of the arguments must always match.

template <class T, class D>
quantity<T,D>
operator+(quantity<T,D> x, quantity<T,D> y)
{
  return quantity<T,D>(x.value() + y.value());
}

template <class T, class D>
quantity<T,D>
operator-(quantity<T,D> x, quantity<T,D> y)
{
  return quantity<T,D>(x.value() - y.value());
}

These operators enable us to write code like:

quantity<float, **length** > len1( 1.0f );
quantity<float, **length** > len2( 2.0f );

len1 = len1 + len2; // OK

but prevent us from trying to add incompatible dimensions:

len1 = len2 + quantity<float, **mass** >( 3.7f ); // **error**

| Prev Next | Back Along | Up Home | Full TOC |