I started to write a C++ template class that would implement strongly-typed ints (so Celsius and Fahrenheit types could behave like ints, but have distinct types).
I gave up after this "simple" idea approached 200 lines of code implementing all the operator overloads. I guess the lesson is that primitives are complex, even if you just want to give them a new name. Also, the expression int/int produces an int, but what should the expression FahrenheitInt/FahrenheitInt produce? A unitless int? A FahrenheitInt?
* Dividing or multiplying a fahrenheit temperature is meaningless, because 0ºF is arbitrary.
* Ideally, you want separate types for absolute temperatures and temperature-deltas. Absolutes cannot be added, multiplied, or divided; subtracting absolutes produces a delta; adding or subtracting an absolute from a delta produces an absolute; deltas can be added and subtracted, and multiplied by unitless values.
* Multiplying a MeterNum by a MeterNum would ideally produce a SquareMeterNum.
* If you don't have to worry about precision, MeterNum and FootNum should both be replaced by LengthNum.
* You can convert a LengthNum into, say, meters by dividing by whatever value represents "1 meter" to produce a unitless int.
* Often operators don't make sense at all. (a << b) only makes sense if b is a unitless integer, for example.
I gave up after this "simple" idea approached 200 lines of code implementing all the operator overloads. I guess the lesson is that primitives are complex, even if you just want to give them a new name. Also, the expression int/int produces an int, but what should the expression FahrenheitInt/FahrenheitInt produce? A unitless int? A FahrenheitInt?