Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #70029

Re: Can "enum" be forced to a certain "bitness"?

From David Brown <david.brown@hesbynett.no>
Newsgroups comp.lang.c
Subject Re: Can "enum" be forced to a certain "bitness"?
Date 2015-09-09 11:07 +0200
Organization A noiseless patient Spider
Message-ID <msosou$m61$1@dont-email.me> (permalink)
References (1 earlier) <87lhcgoh4j.fsf@bsb.me.uk> <2c4d7c17-8b7d-4ea2-aa7d-73d8de39eb49@googlegroups.com> <msnma3$cig$1@dont-email.me> <32a65d36-f768-4d55-94ab-c1582ecddfaf@googlegroups.com> <d59650FldfqU3@mid.individual.net>

Show all headers | View raw


On 09/09/15 00:35, Ian Collins wrote:
> Rick C. Hodgin wrote:
>> On Tuesday, September 8, 2015 at 6:11:11 PM UTC-4, Bart wrote:
>>
>>> You might want extra type protection, but that would need a massive
>>> change to the language (and turn it into C++ or Ada).
>>
>> I think you could use enums to populate a field of type uint16_t and
>> it would populate properly, but I think internally (pre-code-generation)
>> the compiler would probably issue a cast, which is then removed by the
>> optimizer because it's realized that the enum value is really just a
>> constant after all.  I could be wrong about that, but it wouldn't
>> surprise me to learn it is that way.
> 
> Bart is correct.  If you want type safe and fixed size enums, use C++. C
> enums are something of a wasted opportunity.
> 

As Bart says, C enums are not much different from a series of #define
statements.  But they have a few not insignificant advantages over using
#define's:

They let you write neater and more compact enumerations than a series of
defines.  You don't need to give each element a number manually.

They express your intent far better, and thus document the code when you
define the type and use it (even if the compiler can't help enforce this
intent).

As they are not pre-processor macros, they are a little safer in use.
And it is likely that an editor/IDE can help with them (syntax
highlighting, auto-completion, etc.), and debuggers can show values of
an enumerated type by name.

Tools such as documentation tools (doxygen), compilers with static
warning analysis (gcc -Wswitch), other static analysis tools (pclint,
etc.) can all make better use of enum types than #define'd constants.


So they are not a /completely/ wasted opportunity, even though the
compiler itself makes little use of them.

As you say, C++ gives you more type safety and control with "enum class"
types.  But that too is full of wasted opportunity - they should have
supported Ada-style attributes for first, last, successor, predecessor,
iterators, conversion to and from integer (but not implicit conversion),
and they should be usable as the index for arrays.  All things are
possible in C++ with enough templates, of course, but this should have
been part of the "enum class" language feature - or at the very least,
the templates to make it all work should have been standardised.

Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Can "enum" be forced to a certain "bitness"? Ken Brody <kenbrody@spamcop.net> - 2015-09-08 15:07 -0400
  Re: Can "enum" be forced to a certain "bitness"? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-09-08 20:17 +0100
    Re: Can "enum" be forced to a certain "bitness"? Keith Thompson <kst-u@mib.org> - 2015-09-08 12:36 -0700
      Re: Can "enum" be forced to a certain "bitness"? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-09-08 22:52 +0100
      Re: Can "enum" be forced to a certain "bitness"? Ken Brody <kenbrody@spamcop.net> - 2015-09-09 11:40 -0400
    Re: Can "enum" be forced to a certain "bitness"? "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-09-08 14:57 -0700
      Re: Can "enum" be forced to a certain "bitness"? Bartc <bc@freeuk.com> - 2015-09-08 23:10 +0100
        Re: Can "enum" be forced to a certain "bitness"? "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-09-08 15:15 -0700
          Re: Can "enum" be forced to a certain "bitness"? Ian Collins <ian-news@hotmail.com> - 2015-09-09 10:35 +1200
            Re: Can "enum" be forced to a certain "bitness"? David Brown <david.brown@hesbynett.no> - 2015-09-09 11:07 +0200
              Re: Can "enum" be forced to a certain "bitness"? Bartc <bc@freeuk.com> - 2015-09-09 12:36 +0100
                Re: Can "enum" be forced to a certain "bitness"? David Brown <david.brown@hesbynett.no> - 2015-09-09 13:46 +0200
                Re: Can "enum" be forced to a certain "bitness"? Ian Collins <ian-news@hotmail.com> - 2015-09-11 11:32 +1200
                Re: Can "enum" be forced to a certain "bitness"? Keith Thompson <kst-u@mib.org> - 2015-09-10 17:39 -0700
                Re: Can "enum" be forced to a certain "bitness"? David Brown <david.brown@hesbynett.no> - 2015-09-11 09:03 +0200
                Re: Can "enum" be forced to a certain "bitness"? Keith Thompson <kst-u@mib.org> - 2015-09-11 11:33 -0700
                Re: Can "enum" be forced to a certain "bitness"? Ian Collins <ian-news@hotmail.com> - 2015-09-11 21:24 +1200
                Re: Can "enum" be forced to a certain "bitness"? David Brown <david.brown@hesbynett.no> - 2015-09-11 11:38 +0200
                Re: Can "enum" be forced to a certain "bitness"? Richard Heathfield <rjh@cpax.org.uk> - 2015-09-11 10:40 +0100
                Re: Can "enum" be forced to a certain "bitness"? David Brown <david.brown@hesbynett.no> - 2015-09-11 12:51 +0200
                Re: Can "enum" be forced to a certain "bitness"? Les Cargill <lcargill99@comcast.com> - 2015-09-11 12:21 -0500
                Re: Can "enum" be forced to a certain "bitness"? supercat@casperkitty.com - 2015-09-11 15:03 -0700
                Re: Can "enum" be forced to a certain "bitness"? David Brown <david.brown@hesbynett.no> - 2015-09-12 15:01 +0200
                Re: Can "enum" be forced to a certain "bitness"? David Brown <david.brown@hesbynett.no> - 2015-09-11 09:23 +0200
                Re: Can "enum" be forced to a certain "bitness"? David Brown <david.brown@hesbynett.no> - 2015-09-10 10:06 +0200
                Re: Can "enum" be forced to a certain "bitness"? David Kleinecke <dkleinecke@gmail.com> - 2015-09-15 17:55 -0700
        Re: Can "enum" be forced to a certain "bitness"? Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2015-09-08 18:36 -0400
          Re: Can "enum" be forced to a certain "bitness"? Keith Thompson <kst-u@mib.org> - 2015-09-08 16:14 -0700
            Re: Can "enum" be forced to a certain "bitness"? Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2015-09-08 19:49 -0400
              Re: Can "enum" be forced to a certain "bitness"? Keith Thompson <kst-u@mib.org> - 2015-09-08 17:20 -0700
        Re: Can "enum" be forced to a certain "bitness"? Keith Thompson <kst-u@mib.org> - 2015-09-08 15:48 -0700
          Re: Can "enum" be forced to a certain "bitness"? "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-09-08 16:03 -0700
            Re: Can "enum" be forced to a certain "bitness"? Ian Collins <ian-news@hotmail.com> - 2015-09-09 11:16 +1200
              Re: Can "enum" be forced to a certain "bitness"? "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-09-08 16:18 -0700
                Re: Can "enum" be forced to a certain "bitness"? "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-09-08 16:19 -0700
                Re: Can "enum" be forced to a certain "bitness"? Ian Collins <ian-news@hotmail.com> - 2015-09-09 11:42 +1200
                Re: Can "enum" be forced to a certain "bitness"? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-09-09 00:52 +0100
                Re: Can "enum" be forced to a certain "bitness"? David Brown <david.brown@hesbynett.no> - 2015-09-09 13:28 +0200
                Re: Can "enum" be forced to a certain "bitness"? Keith Thompson <kst-u@mib.org> - 2015-09-09 10:56 -0700
                Re: Can "enum" be forced to a certain "bitness"? David Brown <david.brown@hesbynett.no> - 2015-09-09 22:17 +0200
                Re: Can "enum" be forced to a certain "bitness"? Philip Lantz <prl@canterey.us> - 2015-09-08 22:06 -0700
                Re: Can "enum" be forced to a certain "bitness"? Keith Thompson <kst-u@mib.org> - 2015-09-08 23:14 -0700
        Re: Can "enum" be forced to a certain "bitness"? supercat@casperkitty.com - 2015-09-09 16:10 -0700
      Re: Can "enum" be forced to a certain "bitness"? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-09-09 00:42 +0100
        Re: Can "enum" be forced to a certain "bitness"? "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-09-08 16:55 -0700
          Re: Can "enum" be forced to a certain "bitness"? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-09-09 01:31 +0100
            Re: Can "enum" be forced to a certain "bitness"? "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-09-08 17:51 -0700
      Re: Can "enum" be forced to a certain "bitness"? Les Cargill <lcargill99@comcast.com> - 2015-09-08 22:53 -0500
        Re: Can "enum" be forced to a certain "bitness"? Bartc <bc@freeuk.com> - 2015-09-09 10:00 +0100
          Re: Can "enum" be forced to a certain "bitness"? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-09-09 11:27 +0100
            Re: Can "enum" be forced to a certain "bitness"? David Brown <david.brown@hesbynett.no> - 2015-09-09 13:31 +0200
            Re: Can "enum" be forced to a certain "bitness"? Les Cargill <lcargill99@comcast.com> - 2015-09-09 18:32 -0500
              Re: Can "enum" be forced to a certain "bitness"? Keith Thompson <kst-u@mib.org> - 2015-09-09 17:15 -0700
          Re: Can "enum" be forced to a certain "bitness"? Les Cargill <lcargill99@comcast.com> - 2015-09-09 18:29 -0500
            Re: Can "enum" be forced to a certain "bitness"? Keith Thompson <kst-u@mib.org> - 2015-09-09 17:10 -0700
              Re: Can "enum" be forced to a certain "bitness"? Les Cargill <lcargill99@comcast.com> - 2015-09-09 22:27 -0500
                Re: Can "enum" be forced to a certain "bitness"? Keith Thompson <kst-u@mib.org> - 2015-09-09 21:09 -0700
                Re: Can "enum" be forced to a certain "bitness"? Les Cargill <lcargill99@comcast.com> - 2015-09-10 01:54 -0500
                Re: Can "enum" be forced to a certain "bitness"? Keith Thompson <kst-u@mib.org> - 2015-09-10 10:57 -0700
                Re: Can "enum" be forced to a certain "bitness"? James Kuyper <jameskuyper@verizon.net> - 2015-09-10 00:21 -0400
                Re: Can "enum" be forced to a certain "bitness"? Les Cargill <lcargill99@comcast.com> - 2015-09-10 02:00 -0500
                Re: Can "enum" be forced to a certain "bitness"? James Kuyper <jameskuyper@verizon.net> - 2015-09-10 09:32 -0400
      Re: Can "enum" be forced to a certain "bitness"? fir <profesor.fir@gmail.com> - 2015-09-09 08:19 -0700
        Re: Can "enum" be forced to a certain "bitness"? fir <profesor.fir@gmail.com> - 2015-09-09 08:43 -0700
        Re: Can "enum" be forced to a certain "bitness"? "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-09-09 13:05 -0700
          Re: Can "enum" be forced to a certain "bitness"? "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-09-09 13:10 -0700
            Re: Can "enum" be forced to a certain "bitness"? fir <profesor.fir@gmail.com> - 2015-09-10 00:47 -0700
              Re: Can "enum" be forced to a certain "bitness"? fir <profesor.fir@gmail.com> - 2015-09-10 01:05 -0700
                Re: Can "enum" be forced to a certain "bitness"? fir <profesor.fir@gmail.com> - 2015-09-10 02:04 -0700
                Re: Can "enum" be forced to a certain "bitness"? Bartc <bc@freeuk.com> - 2015-09-10 10:30 +0100
                Re: Can "enum" be forced to a certain "bitness"? fir <profesor.fir@gmail.com> - 2015-09-10 06:33 -0700
                Re: Can "enum" be forced to a certain "bitness"? fir <profesor.fir@gmail.com> - 2015-09-10 06:43 -0700
                Re: Can "enum" be forced to a certain "bitness"? fir <profesor.fir@gmail.com> - 2015-09-15 15:22 -0700
              Re: Can "enum" be forced to a certain "bitness"? fir <profesor.fir@gmail.com> - 2015-09-10 01:35 -0700
  Re: Can "enum" be forced to a certain "bitness"? Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-09-08 15:38 -0400
    Re: Can "enum" be forced to a certain "bitness"? Tim Rentsch <txr@alumni.caltech.edu> - 2015-09-09 10:52 -0700
      Re: Can "enum" be forced to a certain "bitness"? David Thompson <dave.thompson2@verizon.net> - 2015-09-20 18:35 -0400
        Re: Can "enum" be forced to a certain "bitness"? Tim Rentsch <txr@alumni.caltech.edu> - 2015-09-26 16:30 -0700
        Re: Can "enum" be forced to a certain "bitness"? Tim Rentsch <txr@alumni.caltech.edu> - 2015-09-26 16:51 -0700
  Re: Can "enum" be forced to a certain "bitness"? James Kuyper <jameskuyper@verizon.net> - 2015-09-08 15:50 -0400
  Re: Can "enum" be forced to a certain "bitness"? Jorgen Grahn <grahn+nntp@snipabacken.se> - 2015-09-08 20:25 +0000
  Re: Can "enum" be forced to a certain "bitness"? David Brown <david.brown@hesbynett.no> - 2015-09-08 23:09 +0200

csiph-web