I would like to create lightweight types that encapsulate a primitive numeric type:
struct A { long value; } struct B { long value; } struct C { long value; } ...
so that I can apply the usual arithmetic operations for each type with the expected results (and without any overhead at runtime compared to the built-in long type):
A a1 {10}; A a2 {20}; A a3 = a1 + a2: ++a3; std::cout << a3;
However, I do not want any (automatic) conversions between different types, and I do not want to allow any arithmetic operations that mix different types. For example, the following code should not compile:
A a1 {10}; A a2 {20}; B b3 = a1 + a2:
Now all this would be simple if I wanted only one type; just define the appropriate operations for class A. However, it will soon become tedious if I try to do the same for classes A, B, C, ... etc. that differ only in name.
I know that I can use preprocessor macros to create multiple copies with different names. However, I was wondering if there is a more elegant approach that does not use a preprocessor and does not require code duplication. (C ++ 11 specific solutions are fine.)
source share