2 ms·
C should have an inferred integer type: for (infer x = 0; x != 10; ++x) ... If the compiler can infer the type, use the fastest integer. If it can not infer,
by jhallenworld 11y ago
C should have an inferred integer type:
for (infer x = 0; x != 10; ++x) ...
If the compiler can infer the type, use the fastest integer. If it can not infer, use the largest integer.
I'm sure this would create difficulties with structs, but at least for local integers it would be nice.
- bluetomcat 11y agoEven if you use int in that case, the compiler is free to pick the optimal underlying data type (byte, probably), provided that it has proven that the value will never go out of that data type's limits and the program doesn't do anything fancy like casting the int to void * and reading it byte by byte.
- jhallenworld 11y agoInteresting, so it's a waste of time to use anything but intmax_t.
- wyldfire 11y agoint8_fast_t meets this description (because the range for this particular loop only spans 10, but you should use the int16_ or int32_ versions as appropriate). int8_fast_t will likely use native-register-sized integers (probably 32 or 64 bit on many architectures). This is IMO very close to the inference you describe, while still providing some stipulations about what you need. int8_fast_t and its siblings are defined in <stdint.h>, described in this article (though it would be nice if the article mentioned them).