Boost logo

Boost :

From: Daryle Walker (darylew_at_[hidden])
Date: 2003-11-18 04:45:10

On 11/16/03 3:19 PM, "Andy Little" <andy_at_[hidden]> wrote:

> ie The type does both initialisation by, and assignment from, another pq
> ...
> ( both pqs MUST have equivalent Powers of Dimension, ie you can only
> assign_to/init_by a length a length, a force a force etc,
> BUT units that pq is expressed in can be different ):
> int main()
> {
> // default ctor
> pq_velocity<>::m_per_minute v1;
> std:: cout << "init by default ctor, v1 <-- "<< v1 <<'\n';
> // explicit value ctor, note1,note2
> pq_velocity<>::mm_per_s v2(1000/60.0f);
> std:: cout << "init by value ctor, v2 <-- "<< v2 <<'\n';
> // "implicit conversion ctor " aka Daryle Walker
> pq_velocity<>::miles_per_hour v3 = v2;
> std::cout << "init by impl conv ctor, v3 <-- " << v3 <<'\n';
> // ..... ok so far AND ... assignment is allowed too ....
> v1 = v3;
> std:: cout << "v1 after assignment by v3, v1 <-- "<< v1 <<'\n';
> //..............
> }
> ///////////////
> output.
> init by default ctor, v1 <-- 0 m.minutes-1
> init by value ctor, v2 <-- 16.6667 mm.s-1
> init by impl conv ctor, v3 <-- 0.0372823 miles.hours-1
> v1 after assignment by v3, v1 <-- 1 m.minutes-1
> /////////////////////////////
> note1: value gives easy mental conv to m_per_minute
> note2: implicit value ctor is obviously not allowed,ie
> pq_velocity<>::mm_per_s v2 = 1000/60.0f; // Error! Destroys dimensional
> analysis )
> OK To explain whats going on here

I know how dimension/unit types work.

If your actual work is structured like what you have in your example, then
you have a bunch of nested types within a class template, right? I guess
the outer template is for the scalar type (can't tell since you use
defaults), and the inner type is the actual unit. I think other libraries
of this type have the scalar type and units at an equal level.

I'm not sure that the system you have here is extensible. You also have
strange combinations of units (mm/s, m/minute). This implementation forces
you to provide every potential combination in the class, which seems to be a
lot of busy work.

You provide "easy" conversion between different units of the same dimension,
but what about different scalar types (of the same units and/or dimension)?
How do you provide the unit conversions? You can't use template parameters
if a conversion isn't a pure integer (I think meters <-> miles is like


Boost list run by bdawes at, gregod at, cpdaniel at, john at