[units] Creating a velocity unit

Boost Community, I'm trying to create a custom velocity unit. I would like to create a unit that is in nautical miles per hour to represent my speed value. If I use pound_force.hpp as an example of units with more than one dimension I can create the following. BOOST_UNITS_DEFINE_BASE_UNIT_WITH_CONVERSIONS(metric, nautical_mile_per_hour, "nautical mile per hour", "nmi h^-1", factor, si::velocity, ???); If this is the right track, what id would I use and how do I know that it is unique? This seems forced. I would rather define this unit in terms of meters per second. Nautical miles is already in terms of meters and hours is also defined as a scale of seconds. Can I somehow use these two already defined units to create the new unit? How would I do this? Ryan

I previously had a similar question where I had to define my own unit that
may be helpful to you:
http://stackoverflow.com/questions/4322133/converting-units-in-boost-units-f...
Cheers!
Andrew Hundt
On Wed, Feb 20, 2013 at 10:11 PM, Steven Watanabe

Thanks for the link it was helpful. I tried both methods and they both
compiled. It seems we can use the simpler form of decltype because it
provides perfect forwarding. Is this a correct interpretation?
Neither of these two typedef seems to behave as I expected. When creating
a quantity with these typedefs I had to call them as a method to get the
code to compile.
quantitysi::velocity a(2.3 * nautical_miles_per_hour1());
quantitysi::velocity b(2.4 * nautical_miles_per_hour2());
quantitysi::velocity c(2.5 * si::meters_per_second);
Is there a way to get my type of nautical_miles_per_hour to behave like
meters_per_second?
Ryan
//Provided for the Discussion
using namespace boost::units;
typedef metric::nautical_mile_base_unit::unit_type nautical_mile_unit;
typedef metric::hour_base_unit::unit_type hour_unit;
typedef decltype(nautical_mile_unit() / hour_unit())
nautical_miles_per_hour1;
typedef divide_typeof_helper

AMDG On 02/21/2013 10:25 AM, Ryan wrote:
Yes. The two are equivalent. decltype is a bit nicer, as long as you know that you'll only be using a compiler that supports it.
The difference is that meters_per_second is an object, not a type: static const si::velocity meters_per_second; At this point, I prefer to use the type, because I don't like having to come up with two different names for the type and the global constant. I suppose that it would also be possible to use only the object, and use decltype whenever you need a type.
In Christ, Steven Watanabe

I see.
That is a pain. I suppose that it would also be possible to use only the
object, and use decltype whenever you need a type.
I tried using the decltype to get the type but got an error. Is this how
you meant to use it?
typedef metric::nautical_mile_base_unit::unit_type nautical_mile_unit;
static const nautical_mile_unit nautical_mile;
quantity

quantity

AMDG On 02/21/2013 01:56 PM, Ryan wrote:
There is no such macro (and it can't be done by any other means). If you want one, there's nothing stopping you from writing one yourself. Anything that I could write would need to be generic enough that it wouldn't save you anything. Also, I should point out that you don't need the intermediate typedef. BOOST_UNITS_STATIC_CONSTANT(nautical_mile, metric::nautical_mile_base_unit::unit_type); will work just fine. In Christ, Steven Watanabe

Also, I should point out that you don't need the intermediate typedef.
BOOST_UNITS_STATIC_CONSTANT(nautical_mile,
metric::nautical_mile_base_unit::unit_type); will work just fine.
This was what I was looking for. I found that BOOST_UNITS_STATIC_CONSTANT needs to be declared outside of the class. I think is due to the namespace the macro uses. I couldn't though use it to declare nautical_miles_per_hour. BOOST_UNITS_STATIC_CONSTANT(nautical_miles, metric::nautical_mile_base_unit::unit_type); BOOST_UNITS_STATIC_CONSTANT(hours, metric::hour_base_unit::unit_type); BOOST_UNITS_STATIC_CONSTANT(nautical_miles_per_hour, decltype(BOOST_TYPEOF(nautical_miles)() / BOOST_TYPEOF(hours)())); //This doesn't compile I could however do the following. static const decltype(BOOST_TYPEOF(nautical_miles)() / BOOST_TYPEOF(hours)()) nautical_miles_per_hour; I don't understand the difference. Ryan

Steven, I didn't notice I was getting a linking error for the following code that we discussed. It compiles fine but has an "unresolved external symbol" for the following. class A { static const metric::nautical_mile_base_unit::unit_type nautical_miles; static const metric::hour_base_unit::unit_type hours; static const decltype(nautical_miles / hours) nautical_miles_per_hour; }; If I move these lines outside the class everything compiles and links fine. What am I missing here? Ryan

at namespace scope in A.cpp:
const metric::nautical_mile_base_unit::unit_type A::nautical_miles;
I don't have a .cpp. Everything is in the header. I tried doing the const thing after the class but it didn't work. Do I have to have a .cpp for it to work? Ryan //The following doesn't work. class A { static const metric::nautical_mile_base_unit::unit_type nautical_miles; static const metric::hour_base_unit::unit_type hours; static const decltype(nautical_miles / hours) nautical_miles_per_hour; } const metric::nautical_mile_base_unit::unit_type A::nautical_miles; const metric::hour_base_unit::unit_type A::hours; const decltype(nautical_miles / hours) A::nautical_miles_per_hour;

AMDG On 02/25/2013 04:33 PM, Ryan wrote:
That will cause a different linker error. (multiple definitions instead of undefined symbol) It takes a bit of magic to define these in a header. BOOST_UNITS_STATIC_CONSTANT handles it, but it only works at namespace scope. The only to to define them inside the class and keep it header only, is to turn A into a template. In Christ, Steven Watanabe
participants (3)
-
Andrew Hundt
-
Ryan
-
Steven Watanabe