Quantcast
Channel: User CPlus - Programming Language Design and Implementation Stack Exchange
Viewing all articles
Browse latest Browse all 66

When can widening conversions cause problems?

$
0
0

I can understand the reason for raising a warning or error when you try to convert a wider integer type to a narrower one, due to the loss of precision.Some C compilers will warn about this:

uint32_t x = 100000;uint16_t y = x;

Swift raises an error:

let y: UInt32 = 100000let x: UInt16 = y // error: cannot convert value of type 'UInt32' to specified type 'UInt16'

Swift also prevents widening implicit conversions even though there is no possibility for overflow or loss of precision:

let y: UInt16 = 100000let x: UInt32 = y // error: cannot convert value of type 'UInt16' to specified type 'UInt32'

Are there any drawbacks to implicit widening conversions? In what cases could allowing them impact program correctness?


Viewing all articles
Browse latest Browse all 66

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>