Hi Bruno, On Wed, Mar 19, 2025 at 06:35:29PM +0100, Bruno Haible wrote: > Alejandro Colomar wrote: > > > >> To address this adoption problem, how about changing these function to > > > >> generic functions (in the sense of <tgmath.h>)? In such a way that > > > >> strtoi (n, &end, base, LONG_MIN, LONG_MAX, &status) > > > >> is known to return a 'long' rather than 'intmax_t', and > > > >> strtoi (n, &end, base, INT_MIN, INT_MAX, &status) > > > > > > That, and especially… > > > > Please propose an implementation of the overload-selectinging macro, and > > clarify how this should work: > > > > n = strto*(s, NULL, 0, SHRT_MIN, UINT_MAX, &status); > > Indeed, the "usual arithmetic conversions" (ISO C 23 § 6.3.1.8, § 7.27.(7)) > would not work well in this case. Instead, one needs to distinguish strtoi > and strtou: > - For strtoi, the first of the types 'signed char', 'short', 'int', 'long', > 'long long', 'intmax_t' that contains both the min and the max value. > - For strtou, the first of the types 'unsigned char', 'unsigned short', > 'unsigned int', 'unsigned long', 'unsigned long long', 'uintmax_t' that > contains both the min and the max value. What if none contain the value? E.g.: n = strtoi(s, NULL, 0, SHRT_MIN, ULLONG_MAX, &status); n = strtou(s, NULL, 0, 3, -1, &status); This first one is a bug, but we should still decide what to do with it. The second one could be a short-hand to say UINTMAX_MAX. Anyway, for this, I'm working on a better API. I don't think we should make strtoi(3) type-generic. a2i() is better for that, accepting the type as a parameter. Cheers, Alex -- <https://www.alejandro-colomar.es/>
Attachment:
signature.asc
Description: PGP signature