J
jlongstreet
Please correct any misconceptions, or voice concerns about this being
a stupid idea in general.
I was wondering today: why doesn't C++ have explicit typedefs?
explicit typedef unsigned int ScreenXCoord;
explicit typedef unsigned int ScreenYCoord;
explicit typedef unsigned int WindowXCoord;
explicit typedef unsigned int WindowYCoord;
ScreenXCoord sx1, sx2;
ScreenYCoord sy1, sy2;
WindowXCoord wx1, wx2;
WindowYCoord wy1, wy2;
....
sx1 = sx2; // ok
sy1 = sy2; // ok
wx1 = wx2; // ok
wy1 = wy2; // ok
sx1 = wy1; // error, converting WindowYCoord to ScreenXCoord
This way, ScreenXCoord != ScreenYCoord != WindowXCoord !=
WindowYCoord.
Many times, it can be unclear what sort of thing a particular value
refers to, even knowing its type, especially with integers, which
could mean width, height, count of bytes, inches, days in a payroll
period, or just about anything else. This is why Hungarian notation
was invented -- cbCount is count of bytes, wxWidth is width in window
coordinates, etc.
Obviously, typedefs are one part of the solution -- it's obvious what
a variable declared as ScreenXCoord is for. But there's no stopping
the programmer from assigning a non-typedef'd unsigned int or another
typedef of unsigned int to that variable.
It seems to me the solution would be to add support for the explicit
keyword to typedef. It would be something like enum in C++,
convertible from their underlying type in some cases, but not the
others.
Once again, please post any criticism (or praise, if I really haven't
overlooked anything).
a stupid idea in general.
I was wondering today: why doesn't C++ have explicit typedefs?
explicit typedef unsigned int ScreenXCoord;
explicit typedef unsigned int ScreenYCoord;
explicit typedef unsigned int WindowXCoord;
explicit typedef unsigned int WindowYCoord;
ScreenXCoord sx1, sx2;
ScreenYCoord sy1, sy2;
WindowXCoord wx1, wx2;
WindowYCoord wy1, wy2;
....
sx1 = sx2; // ok
sy1 = sy2; // ok
wx1 = wx2; // ok
wy1 = wy2; // ok
sx1 = wy1; // error, converting WindowYCoord to ScreenXCoord
This way, ScreenXCoord != ScreenYCoord != WindowXCoord !=
WindowYCoord.
Many times, it can be unclear what sort of thing a particular value
refers to, even knowing its type, especially with integers, which
could mean width, height, count of bytes, inches, days in a payroll
period, or just about anything else. This is why Hungarian notation
was invented -- cbCount is count of bytes, wxWidth is width in window
coordinates, etc.
Obviously, typedefs are one part of the solution -- it's obvious what
a variable declared as ScreenXCoord is for. But there's no stopping
the programmer from assigning a non-typedef'd unsigned int or another
typedef of unsigned int to that variable.
It seems to me the solution would be to add support for the explicit
keyword to typedef. It would be something like enum in C++,
convertible from their underlying type in some cases, but not the
others.
Once again, please post any criticism (or praise, if I really haven't
overlooked anything).