Re: How to elegantly get the enum code from its string type
On Apr 14, 3:06 pm, Keith H Duggar <dug...@alum.mit.edu> wrote:
On Apr 14, 9:27 am, "Leigh Johnston" <le...@i42.co.uk> wrote:
"Kai-Uwe Bux" <jkherci...@gmx.net> wrote in message
The unsigned types are indeed a perfectly fine language
feature; and there are contexts where you absolutely would
want to have them. What causes trouble, every once in a
while, are the rules for conversion and promotion. It is
with those in mind that one has to make the call whether a
variable or a return type should be signed or unsigned.
Opinions differ on what is advantageous in which context.
Probably, a lot depends on the local culture: code should
reflect intent, and different shops can use the
signed/unsigned distinction as slightly different markers.
Yes code should reflect intent and my use of unsigned
integral types reflect the fact that I am using values that
only make sense when positive. If I have values which can
be positive or negative then I will use a signed integral
type.
Alf's assertion that unsigned types indicate "modular
arithmetic" is a nonsense as sizeof(T) does not indicate
"modular arithmetic" and the type of
unsigned types in C++ /ARE/ modular arithmetic. This is
defined by the standard. As to whether they "indicate", well
who knows? Indicate to whom? In what context?
FWIW, and since no one seems to have mentionned it: the real
problem is that arithmetic on unsigned integral types is
modular, and so doesn't follow the rules of natural arithmetic.
If unsigned types are to be used as an abstraction for cardinal
numbers, then either substraction is forbidden, or the results
of substraction are a signed type. Neither is the case for
C++'s unsigned integral types.
I wouldn't limit there use to cases where modular arithmetic is
required---I find them quite useful in cases where *no*
arithmetic makes sense. Regretflly, the standard does use
unsigned types for sizes of objects, but this seems to be a
mistake, since it does make sense to ask the difference between
two sizes (e.g. abs(sizeof(a)-sizeof(b))).
"indicate" aside, it is fact that they are modular arithmetic
and as such "positive" and "negative" lose distinction because
of modular congruence. And that when combined with C++ implicit
conversion rules is the heart of the problem.
Along with the fact that subtraction doesn't give a "natural"
result.
[...]
the sizeof operator is std::size_t which is an unsigned
integral type. std::size_t is used extensively in the real
world so unsigned types are also used extensively in the
real world and not just in "modular arithmetic" contexts.
What exactly do you think this "extensive use" demonstrates?
Did you miss Alf's claim that the "consensus" now is that the
standard got this wrong?
The issue concerning sizeof is somewhat complex. If you're
writing a garbage collector for a 32 bit machine, you need that
extra bit. But such cases are exceptional (and don't justify
the use of an unsigned size_type in the STL).
--
James Kanze