-
Notifications
You must be signed in to change notification settings - Fork 1
Basic Usage
back to home
The following examples are relevant to all units given by the units library.
All units classes are supporting the following constructors:
- default constructor (explicit!)
- double constructor (explicit!)
- copy constructor
- move constructor
Look at the following code for example:
Meters m1; // Default constructor
Meters m2{3}; // Double constructor
Meters m3{m2}; // Copy constructor
Meters m4{std::move(m3)}; // Move constructor
Pay Attention:
The following code will not compile:
Meters m1 = 3; // Will not compile, since double constructor is explicit.
Yards y{3};
Meters m2{y}; // Will not compile, since there is not implicit conversion between different units.
The reason the double constructor is explicit is that we want the user to declare the unit he is using in his code. Using the explicit
keyword of c++ helps us get this effect.
You can add and substract units objects from the same class. Look at the following code for example:
Meters m1{3};
Meters m2{4};
auto m3 = m1 + m2;
auto m4 = -m1;
auto m5 = m1 - m2;
Pay Attention:
The following code won't compile:
Meters m{3};
Yards y{4};
auto m2 = m + y; // Will not compile, since you cannot sum different units.
Meters m3 = -y; // Will not compile, since you can't convert from different units implicitly.
auto m4 = m - y; ; // Will not compile, since you cannot substract different units.
You can multiply or divide every unit with doubles. Those operations represent multiplication or division by scalar. Look at the following code for example:
Meters m1{6};
auto m2 = 3 * m1; // Equals to Meters{18}
auto m3 = m1 * 2.5; // Equals to Meters{15}
auto m4 = m1 / 3; // Equals to Meters{2};
m2 *= 0.5; // m2 is equal to Meters{9} now
m3 /= 5; // m3 is equal to Meters{3} now
Pay Attention:
You cannot use the "*" and "/" to multiply or divide units. In order to do so, use the units_multiply()
and units_ratio()
functions mantioned here
You can compare between units the same way you compare doubles. Look at the following code for example:
Meters m1{3};
Meters m2{4};
if (m1 == m2) cout << "equal!" << endl;
bool bigger = m1 < m2;
units supports all comparison operators:
- ==
- !=
- <
- <=
- >
- >=
Nevertheless, the following code won't compile:
Meters m{3};
Yards y{4};
if (m == y) cout << "equal!" << endl; // Will not compile, since you cannot compare between different units
bool bigger = m < y; // Will not compile, since you cannot compare between different units
All built-in units support printings. When printing a unit it prints the unit name as well. Look at the following code for example:
cout << Meters{3} << endl; // Will print "3 meters"
cout << Yards{4.5} << endl; // Will print "4.5 yards"
cout << MetersPerSecond{-7.123} << endl; // Will print "-7.123 meters/second"
every unit has a user defined literal for it. Look at the following code for example:
cout << 3_meters << endl; // Will print "3 meters"
auto y = -5.2_yards; // Equal to Yards{-5.2}
You can cast from one unit to another using the units_cast()
function. Look at the following code for example:
Meters m{1};
Yards y{1};
Feet f{10};
cout << units_cast<Yards>(m) << endl; // Will print "1.094 yards"
auto m2 = units_cast<Meters>(y); // Equal to Meters{0.144}
auto m3 = units_cast<Meters>(f) - m; // Equal to Meters{2.048}
Pay Attention:
The following code will not compile:
Meters m{1};
cout << units_cast<Knots>(m) << endl; // Will not compile, since you cannot cast untis from different types (speed and length)