I have also been interested in this issue, partially inspired by a
segment of Bjarne Stroustrup's "Going Native" keynote in which he
discusses the programming error at the root of a certain Mars probe's
failure - calculations in one unit system were passed to a function
that expected calculations in another. I think we could all benefit
from a simple way to make differentiated PODs (and, I guess, more
complex types). Now if only coming up with a 'simple solution' were...
well, you know.
One immediate solution I have used was to create a template
`hard_type` which is simply a wrapper that re-implements the operators
exclusively with matching type. In addition to the type it is supposed
to replicate, it takes an number as template argument, which is to be
manually incremented for each differentiation. The C++11 `using` is
used to define the types:
using feet_per_second = hard_type<double,0>;
using meters_per_second = hard_type<double,1>;
This has worked for my little purposes but I can see how it would
quickly become problematic if used in library headers: to boot,
protection would be lost if multiple libraries define a
hard_type<double,0>. Plus who wants to manually keep track of which
number have been used even if all using calls are in the same place.
An internal hash of the new type name might do the trick, but that
would be up to the compiler writer.
Again, the whole purpose of this is to have the compiler reject
mismatched hard types.
Like you, I also had the thought that the keyword `explicit` might be
a good candidate for letting the compiler know that my_type is a T
that is not to be crossed with another T. Something like
using my_type = explicit int;
But whether that's feasible or even practical is beyond my experience.
All of this to say +1 for strong typedefs.
CB