From: Dan W. (danw_at_[hidden])
Date: 2004-01-06 06:34:48


Andy Little wrote:
> ( BTW re your prev example... You Cant use floats as template params... not
> allowed in C++:
...snip...
> So as you say I can never represent real powers here... ie its short
> sighted.

Got it. So Hell with it!: The day someone needs the pi'th power of
brightness, she can use candelas^(22/7); big deal.

> however if I named some ints
> static int const one = 1;
> static int const minus_two = -2;
> I could still represent Force using the names at source code level:
> abstract_pq< one,one,minus_two> Force;

Nah! Forget Force, just make an abstraction, then you can provide a
basic SI units sets one can install like a 'sans serif' font.
You could even dictate the use of double as value type, in such a
supplement (though I'd find that frustrating); --just as long as you
don't dictate a value type at library level.

> But because they are names they are more versatile:
> A simple metaprogramming class
> template< int x>
> struct Rep{
> const static int value = x; // notably 'static' Therefore I do not
> need a class object to get at this name
> };

How about,

  template< int SingleDimTag, std::string DimName >
  struct SingleDimID;
  //where SingleDimTag is a compile time gen. incrementing
  //int to provide dim identification/selection

  template< int SingleUnitTag, std::string UnitName >
  struct SingleDimUnitID;
  //where UnitTag is a compile time gen. incrementing
  //int to provide unit identification/selection

  template< SingleDimID SDID, SingleDimUnitID SDUID >
  struct SingleDimUnit;

  template< SingleDimUnit DU, typename Rep, const Rep val >
  struct SingleDimUnitRep;
  //where Rep specifies the value type, and val the
  //numeric representation for the specific unit.

  template< int N, unsigned int D >
  struct fractional_power;

  template< DimUnitRep, fractional_power >
  struct SingleDimUnitRepPower;

  template< typelist< SingleDimUnitRepPower SDURP > >
  struct NDimAsSingleUnits;
  //note that this is template class that references single
  //dimensional units that compose it, but does not have its
  //own proper unit tag.

  template< int NDimTag, std::string DimName >
  struct NDimID;
  //just to have a proper name for a multi-dim

  template< int NDimUnitTag, std::string UnitName >
  struct NDimUnitID;
  //just to have a proper name for composite or
  //multi-dim unit.

  template< NDimID NDID, NDimUnitID NDUID >
  struct NDimUnit;

  template< NDimAsSingleUnits NDASU, NDimUnit NDU >
  struct NDimProperUnit;
  //to associate a multi-dim unit to a proper unit

  Okay, that's it for me, I need a whole pot of coffee now.

> Using Rep I can redo my names as types:
> typedef one Rep<1>;
> typedef two Rep<2>;
> typedef minus_one Rep<-2>;
> the value is available but with a bit more work:
> int x = one::value; // ie x =1;
> Changing the declaration/def of abstract_pq
> template< typename L, typename T, typename M>
> class abstract_pq{
> private: // nobodys business what goes on in here
> static int const from_p1 = L::value; // extract the value from
> the type... any Type with a suitable 'value' will work
> ...
> };
> and now :
> abstract_pq< 1,1,-2> Force1; // ERROR invalid template
> arguments
> abstract_pq< one,one,minus_two> Force; // ok still works fine using the new
> definitions
> ie names are much more versatile... the Rep types could be replaced by
> Rationals etc
> But some downsides are: it puts a lot of people off (why say 'one', not 1 )
> ...its another layer of complexity... its harder to debug.
> Nevertheless I shall probably have to go that way.

Hope NOT...

>>And the value type has to absolutely be a template type. Someone will
>>want a 7-bit type to make prinatble ascii characters a dimension... you
>>never know.
>
> Oops ....... The value_type is and has always been part of it:

That was a mistake, man; snap out of it :)

>>Template type for now. Don't even bother with such details.
>
> (I Accept that abstract_pq template params may need to be types as above.)
> However I need something that works for me Now, Now, Now
> My major Major criticism of all the other units libs is "Heres My Great
> Lib..."
> then there are a couple of headers of template code
> followed by 'Oh ...BTW... you have to define your own units... dont worry
> it'll be no problem' .

Yeah, I agree that to only provide the bare bones thing is harsh, I
wouldn't mind having SI units or other useful sytems coming with a
library to help me get rolling, but the abstraction underneath is what's
most important to make available, NOT the concrete types.

> Even with integer only powers the thing is useful. I dont need fractional
> powers much... when I do I'll follow my advice... But will watch to see what
> others come up with.
> BTW >>Current noise: nA/(Hz^(0.5)) //"nano-Amps per root-Hertz"
>>>Voltage noise:
> Is very useful.
> Can use this for a 'how to work around limitations of my type' example ...

You DO need fractional powers, IMHO; how else could you have
representations for temporaries in long formulas where arbitrary roots
of expressions may be taken?

> Yep ... Dont have a clue what this SIUnits syntax is about:
> Length L = 5 * meters ;

I find that hateful too, * also used for pointer de-referencing,... It's
painful to the eye; but it's convenient for implementation, I suppose,
as meters my have a numeric representation, e.g. 2^16. I'd even
consider overloading, say, the comma operator:

template < typename T const, std::string const UnitName >
unit operator,( const T number, UnitName name );

then have a macro

#define meters ,meter

so as to be able to write

meter<int> braking_distance = 77 meters;

But there may be precedence collisions to consider, and a macro is a
macro... "meters<int>(77)" would probably be the best compromise.

>>and like I
>>said, what I *needed* it for, in my present project, was to count bytes
>>with a short, and things like that; which I would rather use a typed
>>type, but one that doesn't impose on me that I use double, etc.
> Hmm... sounds like you will have to wait for someones "all singing "
> version... maybe Mathiass Schabel is your man.
> my lib is called *physical*_quantities... it does what its says on the tin.

False Advertising was never a criticism lobbed at your lib. Just that
if you insist on it being just physical, few will have much use for it.

dan