Re:C23 is finished: Here is what is on the menu
by teddyh on Friday August 05, 2022 @11:58AM
(#32357970)
Great, thanks! But that only solves printing. How do I do the reverse operation? I.e. parsing a string to an integer type of an unknown size, while checking for overflow?
Re:C23 is finished: Here is what is on the menu
by Someone on Friday August 05, 2022 @02:04PM
(#32359669)
I think this would work: first, determine the range. For unsigned variants, we have whatever_max = (whatever)(-1), if it isn’t already defined.
That makes parsing the unsigned variant relatively simple. Undefined behavior would be end-of-game, but unsigned addition and multiplication will not invoke that (if you need he,p, look at your favorite multiple-precision library for ideas about detecting wraparound)
For parsing signed integers, derive the min and max values of the signed variant from the max value of the unsigned one ((whatever_max - 1) / 2 or something like that
Then parse your input to the unsigned variant first, then check whether it fits in the unsigned version.
Re:C23 is finished: Here is what is on the menu
by __phantomderp on Friday August 05, 2022 @12:12PM
(#32358171)
¯\_(ツ)_/¯
Someone's got to write a paper to get it in, hopefully based on some sort of existing practice.
https://thephd.dev/c-the-improvements-june-september-virtual...
https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2680.pdf