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


Groups > comp.lang.c > #70199

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

From Ian Collins <ian-news@hotmail.com>
Newsgroups comp.lang.c
Subject Re: Can "enum" be forced to a certain "bitness"?
Date 2015-09-11 11:32 +1200
Message-ID <d5ei9aFt7aaU2@mid.individual.net> (permalink)
References (4 earlier) <32a65d36-f768-4d55-94ab-c1582ecddfaf@googlegroups.com> <d59650FldfqU3@mid.individual.net> <msosou$m61$1@dont-email.me> <msp5fv$ncf$1@dont-email.me> <msp63j$pu1$1@dont-email.me>

Show all headers | View raw


David Brown wrote:
> On 09/09/15 13:36, Bartc wrote:
>>
>> You mean, having arrays that can only be indexed by a particular enum
>> type? That would be very Ada-ish.
>>
>
> Yes, like having:
>
> typedef enum { black, red, green, blue, white } colours;
> typedef struct { uint8_t R; uint8_t G; uint8_t B } rgb;
> static const rgb rgbs[colours] = {
> 	{ 0, 0, 0 }, { 255, 0, 0 }, ...
> }
>
> void foo(colours c) {
> 	set_rgb(rgbs[c]);
> }
>
> I would also like to be able to write "last(colours)" rather than having
> to put an artificial entry "last_colour" into the colours enum (or
> making it as a separate declaration).

The reflections proposals for C++ go a long way to implementing these, 
see N4428.

>> But in C (and in C++ AFAIK), enums are not necessarily sequential. In
>> fact, a set of enums can have arbitrary values, or can all have the same
>> value! That would give with problems with features such as successor and
>> predecessor functions, or even just turn an enum instance value into the
>> same of the value.
>>
>> Although I suspect that more sophisticated uses of enums will come with
>> restrictions.
>>
>
> Exactly.  It would be perfectly reasonable to limit such advanced enum
> use to sequential enums.  Indeed, for many uses of non-sequential enums
> (such as names for bitmasks), C++'s "enum class" would not be suitable
> as you don't have direct access to the underlying number.  I don't think
> it would have been a big loss if "enum class" had been defined as always
> being sequential, starting from 0 - non-sequential enum uses could have
> used old-style "enum".

There is a middle ground that I have already used in embedded C++, typed 
enums (not full blown enum class) such as

enum State : uint16_t { on = 0x001, off = 0x002 };

which allows conversion to the base type

   uint16_t u = State::on;

I don't see why this, being a new syntax, couldn't be adopted in C or 
included as an extension for embedded compilers.

-- 
Ian Collins

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