typedef int: adding operators? (opAdd, etc.)

BCS ao at pathlink.com
Tue Apr 3 22:18:31 PDT 2007


Reply to Charles,

> Is there any way to add operators defined on a type which is defined
> off of the built-in types?
> 
> In particular, it would be nice, e.g., to be able to define types for
> byteNo, wordNo, etc., and be able to add them together (where, say,
> one word == 4 bytes).
> 
> Currently I'm doing this kind of thing on an ad hoc basis, but that
> means that type information is spread out all over the place, and it
> would be nice to concentrate it in one place. (In one instance I'm
> converting between record ID's and disk addresses.  It's easy enough
> to do, but if I change the record size I'll need to track down each
> place where I cast from one type to another.  Not optimal.)
> 
> I suppose I could just write a special function for each conversion,
> and stick them all in the same place, and I may do just that, but this
> approach strikes me as less than ideal.
> 
> I suppose defining:
> ByteNo opAdd(ByteNo b, WordNo w) { ... }
> and then calling it via:
> ByteNo b1 = opAdd(b0, w0);
> isn't horrendous, but it's not as nice as
> ByteNo b1 = b0 + w0;

somewhere there is a compile time units lib that does much of this kind of 
thing. It wraps the internal real type in a struct and forces the conventions 
to get to it. It might not be quite what you want because it would convert 
everything to the same type internally (bytes in your case) and you'd pay 
a price any time you use it as something else. You might take a look anyway.




More information about the Digitalmars-d-learn mailing list