Topic: Default value for enums ?


Author: Valentin Bonnard <Bonnard.V@wanadoo.fr>
Date: 1999/09/28
Raw View
Siemel B. Naran wrote:

> Notice that my text above does not contain the string "valid".

It does contain valid.

Re-read <37ECECAF.30E6@wanadoo.fr>.

--

Valentin Bonnard


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Date: 1999/09/28
Raw View
"Paul D. DeRocco" wrote:
>
> Steve Clamage wrote:
> >
> > Enums have a strange mix of semantics.
>
> True. On the one hand, they look like they were intended for the purpose of
> holding one and exactly one of their enumerators, but they're also commonly
> used for holding ORed combinations of enumerators. In this case, zero might
> be semantically valid, even if there is no zero enumerator:
>
>         enum file_attributes {
>             fa_readonly = 0x01,
>             fa_hidden   = 0x02,
>             fa_system   = 0x04,
>             fa_volume   = 0x08,
>             fa_directory= 0x10,
>             fa_archive  = 0x20
>             };
>
> Some programmers (e.g., at Microsoft) typically define an extra enumerator
> with a value of zero, but I prefer not to. If enumerators can be ORed
> together, it's generally because they are being passed to functions that
> expect ints, not enumerations. In this case, I think "0" more clearly
> indicates "none-of-the-above" than, say, "fa_normal".

For me, nonexistance of a zero enum value would indicate that
"zero" is not one of the intended values (i.e. that you should
give at least one of the enumerated values as parameter).
Also, I'd prefer the functions to take an enum value, and
have the corresponding overloads on the enum.
This also prevents f.ex. to accidentally give a value of 0x40,
where a file_attribute is expected. The fact that you need
overloaded operator| etc. also differentiates between "normal"
enums and "set" enums: "normal" enums won't have the overload
(and therefore passing foo|bar will be an error, since it results
in an int, not an enum).


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: sbnaran@uiuc.edu (Siemel B. Naran)
Date: 1999/09/27
Raw View
On 25 Sep 1999 16:47:30 GMT, Valentin Bonnard <Bonnard.V@wanadoo.fr> wrote:
>Siemel B. Naran wrote:

>> Suppose you had
>>    enum E { e1=1,e2=2,e3=3,e4=4,e5=5};
>>
>> The enum E may also be thought of as
>>    ____ _???
>> In other words, the last 3 bits may be off or on.  So the range of
>> the enum is 0 to 7.  Is this right?
>
>s/valid/intended range (which is 1 to 5)/

What are you talking about?

--
--------------
siemel b naran
--------------
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "Paul D. DeRocco" <pderocco@ix.netcom.com>
Date: 1999/09/27
Raw View
Steve Clamage wrote:
>
> Enums have a strange mix of semantics.

True. On the one hand, they look like they were intended for the purpose of
holding one and exactly one of their enumerators, but they're also commonly
used for holding ORed combinations of enumerators. In this case, zero might
be semantically valid, even if there is no zero enumerator:

 enum file_attributes {
     fa_readonly = 0x01,
     fa_hidden   = 0x02,
     fa_system   = 0x04,
     fa_volume   = 0x08,
     fa_directory= 0x10,
     fa_archive  = 0x20
     };

Some programmers (e.g., at Microsoft) typically define an extra enumerator
with a value of zero, but I prefer not to. If enumerators can be ORed
together, it's generally because they are being passed to functions that
expect ints, not enumerations. In this case, I think "0" more clearly
indicates "none-of-the-above" than, say, "fa_normal".

--

Ciao,                       Paul D. DeRocco
Paul                        mailto:pderocco@ix.netcom.com
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Rich Paul <rich-paul@rich-paul.net>
Date: 1999/09/27
Raw View
That's sed ... string replace.

foo.txt:

now is the time


after s/now is/that was/

foo.txt

that was the time

"Siemel B. Naran" wrote:
>
> On 25 Sep 1999 16:47:30 GMT, Valentin Bonnard <Bonnard.V@wanadoo.fr> wrote:
> >Siemel B. Naran wrote:
>
> >> Suppose you had
> >>    enum E { e1=1,e2=2,e3=3,e4=4,e5=5};
> >>
> >> The enum E may also be thought of as
> >>    ____ _???
> >> In other words, the last 3 bits may be off or on.  So the range of
> >> the enum is 0 to 7.  Is this right?
> >
> >s/valid/intended range (which is 1 to 5)/
>
> What are you talking about?
>
> --
> --------------
> siemel b naran
> --------------
> ---
> [ comp.std.c++ is moderated.  To submit articles, try just posting with ]
> [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
> [              --- Please see the FAQ before posting. ---               ]
> [ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: sbnaran@uiuc.edu (Siemel B. Naran)
Date: 1999/09/27
Raw View
On 27 Sep 99 15:49:50 GMT, Rich Paul <rich-paul@rich-paul.net> wrote:
>> On 25 Sep 1999 16:47:30 GMT, Valentin Bonnard <Bonnard.V@wanadoo.fr> wrote:
>>> Siemel B. Naran wrote:

>>>> Suppose you had
>>>>    enum E { e1=1,e2=2,e3=3,e4=4,e5=5};
>>>>
>>>> The enum E may also be thought of as
>>>>    ____ _???
>>>> In other words, the last 3 bits may be off or on.  So the range of
>>>> the enum is 0 to 7.  Is this right?
>>>
>> >s/valid/intended range (which is 1 to 5)/

Notice that my text above does not contain the string "valid".

>> What are you talking about?


>That's sed ... string replace.

I didn't know text Valentin wanted me to apply the search/replace to.

--
--------------
siemel b naran
--------------
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: clamage@eng.sun.com (Steve Clamage)
Date: 1999/09/25
Raw View
sbnaran@uiuc.edu (Siemel B. Naran) writes:

>On 24 Sep 1999 16:24:59 GMT, Philip Brabbin <pabrabbin@hotmail.com> wrote:

>>Now what is the value for data_type() if data_type is an enum ?  My
>>compiler gives 0 even if 0 is not a valid value for the enum type.

>I thought 0 was always valid for an enum.

Yes. An enum value must be represented in pure binary (like
integers). Any value that requires no more bits to represent
that the defined enumerators is a valid value of the enum.
Since 0 can always be represented in the same number of bits
as any enumerator, it is always a valid value.

But I think the orginal poster meant to say that 0 might
not be a defined enumerator.

Enums have a strange mix of semantics.

--
Steve Clamage, stephen.clamage@sun.com


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Gene Bushuyev" <gbush@my-deja.com>
Date: 1999/09/27
Raw View
Siemel B. Naran <sbnaran@uiuc.edu> wrote in message
news:slrn7unen6.4d5.sbnaran@localhost.localdomain...
> On 24 Sep 1999 16:24:59 GMT, Philip Brabbin <pabrabbin@hotmail.com> wrote:
>
> >Now what is the value for data_type() if data_type is an enum ?  My
> >compiler gives 0 even if 0 is not a valid value for the enum type.
>
> I thought 0 was always valid for an enum.
>
> Suppose you had
>    enum E { e1=1,e2=2,e3=3,e4=4,e5=5};
>
> The enum E may also be thought of as
>    ____ _???
> In other words, the last 3 bits may be off or on.  So the range of
> the enum is 0 to 7.  Is this right?

Yes, the range of the underlying type is at least 3 bits (0-7), or may be
bigger integral type if the implementation chooses so. It is explained in
7.2p6. Since whatever bit-field can be chosen for underlying implementation
it always contains 0...0 number then 0 (of corresponding integral type) is
always a valid initializer for enum. It's true even in case of empty enum
(7.2p5)

Gene Bushuyev
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Valentin Bonnard <Bonnard.V@wanadoo.fr>
Date: 1999/09/25
Raw View
Siemel B. Naran wrote:

> On 24 Sep 1999 16:24:59 GMT, Philip Brabbin <pabrabbin@hotmail.com> wrote:
>
> >Now what is the value for data_type() if data_type is an enum ?  My
> >compiler gives 0 even if 0 is not a valid value for the enum type.
>
> I thought 0 was always valid for an enum.
>
> Suppose you had
>    enum E { e1=1,e2=2,e3=3,e4=4,e5=5};
>
> The enum E may also be thought of as
>    ____ _???
> In other words, the last 3 bits may be off or on.  So the range of
> the enum is 0 to 7.  Is this right?

s/valid/intended range (which is 1 to 5)/

--

Valentin Bonnard


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: sbnaran@uiuc.edu (Siemel B. Naran)
Date: 1999/09/25
Raw View
On 24 Sep 1999 16:24:59 GMT, Philip Brabbin <pabrabbin@hotmail.com> wrote:

>Now what is the value for data_type() if data_type is an enum ?  My
>compiler gives 0 even if 0 is not a valid value for the enum type.

I thought 0 was always valid for an enum.

Suppose you had
   enum E { e1=1,e2=2,e3=3,e4=4,e5=5};

The enum E may also be thought of as
   ____ _???
In other words, the last 3 bits may be off or on.  So the range of
the enum is 0 to 7.  Is this right?

--
--------------
siemel b naran
--------------
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Philip Brabbin <pabrabbin@hotmail.com>
Date: 1999/09/24
Raw View
Hi,

When writing template code, it's sometimes necessary to refer to the
"default value" of a template type.  For example operator[]() for
std::map could be defined as:

    (*((m.insert(value_type(k, data_type()))).first)).second

where "data_type" is the template parameter.

If data_type is an integral value, then data_type() will be 0.
If data_type is a floating-point value, then data_type() will be 0.0.

Now what is the value for data_type() if data_type is an enum ?  My
compiler gives 0 even if 0 is not a valid value for the enum type.

Is there any way to specify this value for enumeration types to ensure
that illegal values never occur ?

- Phil


Sent via Deja.com http://www.deja.com/
Before you buy.


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: comeau@panix.com (Greg Comeau)
Date: 1999/09/24
Raw View
In article <7sfrek$vhe$1@nnrp1.deja.com> Philip Brabbin <pabrabbin@hotmail.com> writes:
>When writing template code, it's sometimes necessary to refer to the
>"default value" of a template type.  For example operator[]() for
>std::map could be defined as:
>
>    (*((m.insert(value_type(k, data_type()))).first)).second
>
>where "data_type" is the template parameter.
>
>If data_type is an integral value, then data_type() will be 0.
>If data_type is a floating-point value, then data_type() will be 0.0.
>
>Now what is the value for data_type() if data_type is an enum ?  My
>compiler gives 0 even if 0 is not a valid value for the enum type.
>
>Is there any way to specify this value for enumeration types to ensure
>that illegal values never occur ?

Surprise.  Of course by now you know the problem isn't really
template specific, but a default init thingie :)  I've long
deplored the magic of 0, in many contexts, even back in "good 'ol C".
Atop of this, it can be argued then enum's are broken in various ways.
None of this is to say T() isn't handy, it is.

- Greg
--
       Comeau Computing, 91-34 120th Street, Richmond Hill, NY, 11418-3214
     Producers of Comeau C/C++ 4.2.38 -- NOTE 4.2.42 BETAS NOW AVAILABLE
    Email: comeau@comeaucomputing.com / Voice:718-945-0009 / Fax:718-441-2310
                *** WEB: http://www.comeaucomputing.com ***


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]