Topic: Defect Report: user-defined conversion and built-inoperator=
Author: James Kuyper <kuyper@wizard.net>
Date: 2000/11/07 Raw View
scott douglass wrote:
...
> [[Aside: It's not clear to me that the all of the possible conversions
> required (e.g. char to short and short to char) are even defined by the
> (C99) standard. The (C99) standard does define the conversions char to int
> and int to short and I can't immediately construct a reasonable example
> where a direct char to short conversion would give a different result from
> the pair of conversions applied in sequence.]]
The C99 standard doesn't define the conversions one pair at a time; all
of the integer conversions are covered by three rules, which apply to
all possible pairs of integer types:
"6.3.1.3 Signed and unsigned integers
1 When a value with integer type is converted to another integer type
other than _Bool,if the value can be represented by the new type, it is
unchanged.
2 Otherwise, if the new type is unsigned, the value is converted by
repeatedly adding or subtracting one more than the maximum value that
can be represented in the new type until the value is in the range of
the new type.49)
3 Otherwise, the new type is signed and the value cannot be represented
in it; either the result is implementation-defined or an
implementation-defined signal is raised."
Plain char could be either signed or unsigned, so I'll cover both cases.
Conversion from signed char to short or int is covered by the rule 1,
since the rules governing the ranges of the different types guarantee
that the value is representable in the new type. Unsigned char to short
or int is covered either by rule 1 or rule 3, depending upon whether or
not the value is greater than INT_MAX (which it can be; UCHAR_MAX can be
> INT_MAX).
The down conversion you mention, from int to short, is governed by rule
1 or rule 3, depending upon whether or not the value is less than
SHORT_MAX.
Therefore, given
char c;
Then
(short)c != (short)(int)c
can only be true if rule 3 applies, making the results
implementation-defined, which is the case if and only if the value of c
is greater than SHORT_MAX (which it can be if char is unsigned).
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]