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


Groups > comp.std.c++ > #240

Re: New strongly type enum proposal featuring inherited enums

From Seungbeom Kim<musiphil@bawi.org>
Newsgroups comp.std.c++
Subject Re: New strongly type enum proposal featuring inherited enums
Date 2011-08-24 14:06 -0700
Organization albasani.net
Message-ID <j2fuat$u6f$1@usenet.stanford.edu> (permalink)
References <4e49ea26$2@news.x-privat.org>

Show all headers | View raw


On 2011-08-16 20:28, Phil Bouchard wrote:
>
>  Is it too late to change the strongly type enum?  I have a proposal
>  that is much cleaner than what is on its way.

I'm afraid it is too late to change anything for C++0x.
But you can certainly propose something for the next standard.

>  Right now we have:
>  enum class Val: unsigned long { E1, E2, E3, E4 };
>
>  Comments:
>  1) This design is bad because the ':' sign is reserved for inheritance.

The ':' sign had not been reserved for inheritance. Labels (including
case labels) use it, bit field declarations use it, ctor-initializers
use it, the ternary operator (?:) use it, and so on.

Furthermore, extending the meaning of a lexical element is common
as the language evolves.

>
>  2) Inheriting from another enum would be quite useful.  For example:
>  enum class Widget {button, combobox, listview};
>  enum class SuperWidget : Widget {iconview}
>
>  This way iconview would be equal to 3, not 0.

When class D inherits from class B, any D object can be regarded as a
B object; e.g. you can pass a D object to a function like f(B&).
Therefore, D is a subtype of B, i.e. D<: B.

When enum class SW "inherits" from enum class W, a SW object cannot be
regarded as a W object, but the converse holds: i.e. not SW<: W, but
W<: SW. In this regard, using the term "inheritance" for extending enum
classes as such may be confusing.

But this could be useful, still. I can even imagine something like this:

     enum class WindowOptions : unsigned short { A, B, C };
     enum class AdvancedWindowOptions : WindowOptions { D, E };

whose implications in full I'm not sure of yet.

>
>  3) Redefining the underlying type could be easily done with:
>  enum class Val { unsigned long E1, E2, E3, E4 };
>
>  This is much more neat and is similar to a declaration of multiple instances:
>  const unsigned long E1, E2, E3, E4;

It looks similar to a simple-declaration, but its syntax must be
something entirely different. You can neither put a semicolon at
the end of the list of the enumerators, nor use different types
for different enumerators.
I think this is a case where a syntax that is too similar but not the
same can be more confusing than a syntax that looks different enough.
We might face questions like '"int i1, i2;" is equivalent to "int i1;
int i2;" but why isn't it the case inside an enum?'

Anyway, this is a matter of taste, after all.

-- 
Seungbeom Kim


[ comp.std.c++ is moderated.  To submit articles, try posting with your ]
[ newsreader.  If that fails, use mailto:std-cpp-submit@vandevoorde.com ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]

Back to comp.std.c++ | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

New strongly type enum proposal featuring inherited enums Phil Bouchard <philippe@fornux.com> - 2011-08-16 21:28 -0600
  Re: New strongly type enum proposal featuring inherited enums Daniel Krügler <daniel.kruegler@googlemail.com> - 2011-08-18 07:54 -0600
    Re: New strongly type enum proposal featuring inherited enums Phil Bouchard<philippe@fornux.com> - 2011-08-24 14:06 -0700
      Re: New strongly type enum proposal featuring inherited enums Daniel Krügler<daniel.kruegler@googlemail.com> - 2011-08-24 17:39 -0700
        Re: New strongly type enum proposal featuring inherited enums Bart van Ingen Schenau<bart@ingen.ddns.info> - 2011-08-25 20:51 -0600
          Re: New strongly type enum proposal featuring inherited enums =3D?ISO-8859-15?Q?Daniel_Kr=3DFCgler?=3D <daniel.kruegler@googlemail.= com> - 2011-09-07 11:08 -0700
            Re: New strongly type enum proposal featuring inherited enums Miles Bader <miles@gnu.org> - 2011-09-08 14:46 -0700
              Re: New strongly type enum... why not use "using"? "peter miller"<fuchsia.groan@virgin.net> - 2011-09-12 11:18 -0700
                Re: New strongly type enum... why not use "using"? Miles Bader<miles@gnu.org> - 2011-09-13 09:29 -0700
                Re: New strongly type enum... why not use "using"? "peter miller"<fuchsia.groan@virgin.net> - 2011-09-15 14:13 -0700
  Re: New strongly type enum proposal featuring inherited enums Pete Becker <pete@versatilecoding.com> - 2011-08-18 07:51 -0600
    Re: New strongly type enum proposal featuring inherited enums Victor Bazarov<v.bazarov@comcast.invalid> - 2011-08-24 14:06 -0700
    Re: New strongly type enum proposal featuring inherited enums Paavo Helde<myfirstname@osa.pri.ee> - 2011-08-24 14:06 -0700
    Re: New strongly type enum proposal featuring inherited enums Francis Glassborow<francis.glassborow@btinternet.com> - 2011-08-24 14:52 -0700
      Re: New strongly type enum proposal featuring inherited enums Phil Bouchard<philippe@fornux.com> - 2011-08-24 17:39 -0700
  Re: New strongly type enum proposal featuring inherited enums Alain Ketterlin <alain@dpt-info.u-strasbg.fr> - 2011-08-18 07:53 -0600
    Re: New strongly type enum proposal featuring inherited enums Phil Bouchard<philippe@fornux.com> - 2011-08-24 14:52 -0700
  Re: New strongly type enum proposal featuring inherited enums Marcel Müller <news.5.maazl@spamgourmet.com> - 2011-08-18 07:52 -0600
    Re: New strongly type enum proposal featuring inherited enums Phil Bouchard<philippe@fornux.com> - 2011-08-23 18:20 -0700
    Re: New strongly type enum proposal featuring inherited enums Phil Bouchard <philippe@fornux.com> - 2011-08-24 14:52 -0700
    Re: New strongly type enum proposal featuring inherited enums Phil Bouchard <philippe@fornux.com> - 2011-08-24 14:52 -0700
  Re: New strongly type enum proposal featuring inherited enums Francis Glassborow<francis.glassborow@btinternet.com> - 2011-08-24 14:06 -0700
  Re: New strongly type enum proposal featuring inherited enums "Bo Persson"<bop@gmb.dk> - 2011-08-24 14:06 -0700
  Re: New strongly type enum proposal featuring inherited enums red floyd<no.spam.here@its.invalid> - 2011-08-24 14:06 -0700
  Re: New strongly type enum proposal featuring inherited enums Seungbeom Kim<musiphil@bawi.org> - 2011-08-24 14:06 -0700
    Re: New strongly type enum proposal featuring inherited enums Phil Bouchard<philippe@fornux.com> - 2011-08-24 17:39 -0700
    Re: New strongly type enum proposal featuring inherited enums Phil Bouchard<philippe@fornux.com> - 2011-08-24 17:40 -0700
    Re: New strongly type enum proposal featuring inherited enums Phil Bouchard<philippe@fornux.com> - 2011-08-24 17:40 -0700

csiph-web