Douglas A. Gwyn said:
Keith Thompson wrote: [...]
I have some vague thoughts about adding such a facility to a future
version of C (letting a user declare an integer type with a specified
range), but I don't see much demand for it.
There won't be much demand for anything that won't be
widely supported. That's why it makes sense to work
for such improvements within the context of the language
standard.
I was thinking of something that would have to be in the standard
itself, since it would involve new syntax. (I suppose it could be
implemented as an extension, but such an extension couldn't be used
with any compiler that doesn't support it -- unlike, say, <stdint.h>,
which can be implemented in pre-C99 code.) My thought is that there
isn't enough demand to get it into the standard in the first place.
Here's the idea. Allow a new kind of type specifier, something like
signed(-1000, 1000)
unsigned(0, 255)
I'm not sure that's the best syntax; I'm open to suggestions. The
arguments specify the range that must be supported by the type. It
acts like a typedef for some predefined signed or unsigned type that
includes the requires range. The resulting type is not constrained by
the specified range; the range is used only to select which predefined
type to use.
The lower bound isn't strictly necessary for unsigned types, but I
think it should be required for symmetry with signed types.
The type signed(-32768, 32767) would be at least 16 bits on a
2's-complement system, and more than 16 bits on a 1's-complement or
signed-magnitude system.
I think that unsigned(0, 255) should be equivalent to uint_fast8_t.
There should probably be another syntax for something equivalent to
uint_least8_t. I don't think it makes sense for a type with a range
specification to require an exact size (as uint8_t does), but I
wouldn't mind another syntax for specifying an exact number of bits.
Maybe just signed(16) and unsigned(32).
The C++ folks might argue that the syntax should be unsigned<0, 255>,
since this is similar in concept to C++ templates -- but if a future
C++ standard were to adopt this for compatibility with C, the dual use
of the <> syntax could cause more confusion. Or not; if this idea
goes anywhere, someone should look into the C/C++ compability issues.
(Compatibility with C++ is not required, of course, but if we can get
it easily we should.)
Probably the only way to guarantee that this would be accepted in a
new standard would be to implement it via another overloading of the
"static" keyword.
}