Suppose there are two types:
typedef unsigned short Altitude;
typedef double Time;
In order to detect some errors, such as the transfer time argument in the height position for functions at compile time, I would like to prohibit implicit conversion from Altitudeto Timeand vice versa.
At first I tried to declare operator Altitude(Time)without an implementation, but the compiler said that it should be a member function, so I realized that it would not work for type typedefed.
Next, I tried to turn one of these types in the class, but it turned out that the project makes extensive use of many arithmetic operations, including the implicit conversion double, int, booletc., and transmits them to flow through and out of operator<<and operator>>. Therefore, despite this method, I was able to find the errors that I was looking for, I did not even try to fully implement a compatible class, because this would require a lot of code.
So my question is: is there a more elegant way to prevent implicit conversions between two specific typedefed types , and if so, how?
source
share