Topic: Conversion between Integer Types


Author: dag@control.lth.se (Dag Bruck)
Date: 24 Oct 1994 06:57:36 GMT
Raw View
>>>>> "TH" == Tommi H|yn{l{nmaa <tohoyn@janus.otol.fi> writes:

TH> The implicit integer type conversions in C++ do not currently give
TH> an error if the number to be converted is out of range.  ....
TH> I think that runtime errors (or exceptions) should be generated in
TH> these kind of cases.

Of course you are right!  I think the working paper says the behaviour
is "implementation defined" when a signed conversion is out of range,
which indeed may be to throw an exception.

Go ask your compiler vendor!

      -- Dag




Author: tohoyn@janus.otol.fi (Tommi H|yn{l{nmaa)
Date: 23 Oct 1994 16:43:30 GMT
Raw View
The implicit integer type conversions in C++ do not currently give an error
if the number to be converted is out of range.
This problem occurs when integer types of different size are converted to
each other, but also with signed and unsigned integer types of same size.

e.g.
  int n1;
  unsigned int n2;
  n1 = -1; // n1 == -1
  n2 = n1; // out of range: n2 == 2^32 - 1 (with 32-bit ints)

  int n1;
  signed char n2;
  n1 = 1000; // n1 == 1000
  n2 = n1; // out of range: n2 == -24

I think that runtime errors (or exceptions) should be generated in these
kind of cases. However, "low-level" conversions between integer types may
sometimes be needed. Therefore, there should be special conversion functions
that would behave like the present conversions.

The implicit conversion from a floating point number to an integer should
also be removed and replaced with a function.

P.S. Where can I find the Working Paper???






Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 24 Oct 1994 16:31:50 GMT
Raw View
In article rgu@tethys.otol.fi, tohoyn@janus.otol.fi (Tommi H|yn{l{nmaa) writes:
>
>The implicit integer type conversions in C++ do not currently give an error
>if the number to be converted is out of range.
>This problem occurs when integer types of different size are converted to
>each other, but also with signed and unsigned integer types of same size.

This characterization is not correct.

When converting to a signed integral type which cannot hold the actual
value to be converted, the results are implementation-defined. An
implemenation may give a compile-time warning (or error), or a runtime
notification, such as a signal or exception. The implementation
is not required to do any of these things, however.

When converting to an unsigned integral type, the results are completely
specified, and neither truncation nor a change in value is an error.

I can appreciate that for numerical work you would like to be notified
of a change in value. I don't expect such a change in the language
definition. Nothing prevents a C++ implementor from providing such
features as an option, of course.
---
Steve Clamage, stephen.clamage@eng.sun.com