J
Jorgen Grahn
I have recently realized that I want more distinct types in the code I
work with, to get better help from the compiler.
The code I work with now is like much real-life code: dozens of
logically different kinds of identities are mapped to simple integer
types, e.g. u_int16_t. These can be various kinds of IDs, like UDP
port numbers. Some of them are my-side/remote-side pairs, where it's
easy to confuse them and e.g. send an UDP datagram to my port rather
than the remote one.
I can do something like:
template<char TAG> struct Id { unsigned value; ... };
typedef Id<'x'> MyId;
typedef Id<'y'> RemoteId;
But that means I have to manually create the distinct tags, and the
compiler cannot detect my mistakes.
- How do people generally accomplish the thing I want?
- How do people do it in the more general case, when the real values
are of class type? For example, if my application uses
std::strings, but want to classify them in three different, distinct
types?
- Am I too fixated on static type checking?
/Jorgen
work with, to get better help from the compiler.
The code I work with now is like much real-life code: dozens of
logically different kinds of identities are mapped to simple integer
types, e.g. u_int16_t. These can be various kinds of IDs, like UDP
port numbers. Some of them are my-side/remote-side pairs, where it's
easy to confuse them and e.g. send an UDP datagram to my port rather
than the remote one.
I can do something like:
template<char TAG> struct Id { unsigned value; ... };
typedef Id<'x'> MyId;
typedef Id<'y'> RemoteId;
But that means I have to manually create the distinct tags, and the
compiler cannot detect my mistakes.
- How do people generally accomplish the thing I want?
- How do people do it in the more general case, when the real values
are of class type? For example, if my application uses
std::strings, but want to classify them in three different, distinct
types?
- Am I too fixated on static type checking?
/Jorgen