2

I'm trying to assign both metres and millimetres to boost::units variables. However, it seems like boost::units does not easily convert from millimetres to metres. The code I'm using is

boost::units::quantity<boost::units::si::length> lenA = 2.0 * boost::units::si::metres;
boost::units::quantity<boost::units::si::length> lenB = static_cast<tracking::units::Length>(2.0 * boost::units::si::milli * boost::units::si::metres);

(using a static_cast) but I would like to drop the cast and just use

boost::units::quantity<boost::units::si::length> lenA = 2.0 * boost::units::si::metres;
boost::units::quantity<boost::units::si::length> lenB = 2.0 * boost::units::si::milli * boost::units::si::metres;

How do I do this (or at least make it easier to code)?

brewmanz
  • 1,181
  • 11
  • 17
  • What is `tracking::units::Length`? By the way, if you want something "easier to code," maybe just use int64_t millimeters as your standard units or something. Anything would be easier than this! – John Zwinck Oct 09 '14 at 00:56
  • Does this help? http://stackoverflow.com/questions/14724063/millimeters-in-boostunits – sehe Oct 09 '14 at 09:13
  • Also, related to FAQ entry http://www.boost.org/doc/libs/1_56_0/doc/html/boost_units/FAQ.html#boost_units.FAQ.NoConstructorFromValueType – sehe Oct 09 '14 at 09:19

1 Answers1

0

You should include <boost/units/systems/si/prefixes.hpp>. It will allow you to use prefixes:

quantity<length> len2(2.0 * milli * meter);

Sample Code:

#include <stdlib.h>
#include <string>
#include <iostream>
#include <boost/units/systems/si/prefixes.hpp>
#include <boost/units/systems/si/length.hpp>
#include <boost/units/systems/si/io.hpp>

using namespace std;
using namespace boost::units;
using namespace boost::units::si;
using namespace boost;

int main(int argc, char* argv[]) {

  quantity<length> len1(2.0 * meter);
  quantity<length> len2(2.0 * milli * meter);
  quantity<length> len3(2.0 * centi * meter);

  cout << len1 << endl << len2 << endl << len3 << endl;

  return EXIT_SUCCESS;
}

And the result is:

2 m
0.002 m
0.02 m
Grigorii Chudnov
  • 3,046
  • 1
  • 25
  • 23