Re: Storing units in the database

From: Misha Dorman <misha_at_no_mishapen_spam.co.uk>
Date: Thu, 21 Jul 2005 20:47:54 +0100
Message-ID: <11dvupccebj6ffc_at_corp.supernews.com>


Mikito Harakiri wrote:
> Misha Dorman wrote:

>>http://www.awprofessional.com/articles/article.asp?p=375705&seqNum=1,

> Now the question to ask is do those templates, metafunctions and
> lambdas really simpler than, say
>
> 20 * kg * 30 * m / c^2

Its quite simple[1] to set up a system so that expression is checked for dimensional/unit correctness and type safety (statically) using the TMP approach. Basically, just make the constructors-from-basic-types of the dimensioned type be protected, define constant static members of a friend "units" class named "kg", "m", "c" etc. (with the relevant values), and define a bunch of overloaded friend operators for *, /, +, - etc. taking mixtures of quantities and scalars to allow expressions like 20 * kg.

Then you can write something like:

quantity<float,mass> m = 10 * units::kg;
quantity<float,accel> g = 9.81 * units::m / (units::sec*units::sec)
quantity<float,force> f = m*a; //Note units are "implicit"

but not, say:

quantity<float,force> g = 9.81;

                //ERROR -- no units (attempt to use protected ctor)

quantity<float,force> f = m/a; //ERROR -- incorrect dimensions

Misha

[1] In C++ terms, that is -- at best that generally means "horribly complex to write, but at least it can be tested easily and used painlessly". Received on Thu Jul 21 2005 - 21:47:54 CEST

Original text of this message