Topic: Scoped enums: default value
Author: viboes <vicente.botet@wanadoo.fr>
Date: Sat, 19 Mar 2011 13:22:27 CST Raw View
Hi,
is the following correct?
enum class E {E2=2,E3,E4};
E e = E();
assert(int(e)==0);
If yes, it is quite surprising that the default value of an enum
doesn't match any one of the enumerators.
Best,
Vicente
--
[ 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 ]
Author: =?ISO-8859-1?Q?Daniel_Kr=FCgler?= <daniel.kruegler@googlemail.com>
Date: Sun, 20 Mar 2011 14:20:15 CST Raw View
Am 19.03.2011 20:22, schrieb viboes:
>
> Hi,
>
> is the following correct?
>
> enum class E {E2=2,E3,E4};
> E e = E();
> assert(int(e)==0);
>
> If yes, it is quite surprising that the default value of an enum
> doesn't match any one of the enumerators.
Yes, the assert should hold. All enumeration types are scalar types
(see [basic.types] p. 9 in the recent working draft) and the
expression E() is defined for such types, see [expr.type.conv] p. 2
"The expression T(), where T is a simple-type-specifier or
typename-specifier for a non-array complete object type or the
(possibly cv-qualified) void type, creates a prvalue of the specified
type,which is value-initialized"
Value-initialization is defined in [dcl.init] and since an enumeration
type is a scalar type we end up in zero-initialization (p. 5):
"if T is a scalar type (3.9), the object is set to the value 0 (zero),
taken as an integral constant expression, converted to T;"
p. 10 also says:
"An object whose initializer is an empty set of parentheses, i.e., (),
shall be value-initialized."
There is nothing special about enumeration types in this regard,
because they are capable to represent all values that their underlying
type can represent, see [dcl.enum] p. 7:
"For an enumeration whose underlying type is fixed, the values of the
enumeration are the values of the underlying type."
(Note that p. 5 explains that the underlying type of a scoped
enumeration type is fixed and p. 6 explains that the underlying type
is an integral type).
HTH & Greetings from Bremen,
Daniel Kr gler
--
[ 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 ]
Author: viboes <vicente.botet@wanadoo.fr>
Date: Tue, 22 Mar 2011 13:26:59 CST Raw View
On Mar 20, 9:20 pm, Daniel Kr gler <daniel.krueg...@googlemail.com>
wrote:
> Am 19.03.2011 20:22, schrieb viboes:
> > Hi,
>
> > is the following correct?
>
> > enum class E {E2=2,E3,E4};
> > E e = E();
> > assert(int(e)==0);
>
> > If yes, it is quite surprising that the default value of an enum
> > doesn't match any one of the enumerators.
>
> Yes, the assert should hold. All enumeration types are scalar types
> (see [basic.types] p. 9 in the recent working draft) and the
> expression E() is defined for such types, see [expr.type.conv] p. 2
>
> "The expression T(), where T is a simple-type-specifier or
> typename-specifier for a non-array complete object type or the
> (possibly cv-qualified) void type, creates a prvalue of the specified
> type,which is value-initialized"
>
> Value-initialization is defined in [dcl.init] and since an enumeration
> type is a scalar type we end up in zero-initialization (p. 5):
>
> "if T is a scalar type (3.9), the object is set to the value 0 (zero),
> taken as an integral constant expression, converted to T;"
>
> p. 10 also says:
>
> "An object whose initializer is an empty set of parentheses, i.e., (),
> shall be value-initialized."
>
> There is nothing special about enumeration types in this regard,
> because they are capable to represent all values that their underlying
> type can represent, see [dcl.enum] p. 7:
>
> "For an enumeration whose underlying type is fixed, the values of the
> enumeration are the values of the underlying type."
>
> (Note that p. 5 explains that the underlying type of a scoped
> enumeration type is fixed and p. 6 explains that the underlying type
> is an integral type).
Hi,
Thanks for showing me the long trip.
I don't know why I believed that with the introduction of scoped enums
the single values an enum class can take is one of the enumerators.
But I see that I was completely wrong and that nothing has been
changed in this respect since eums where introduce in C.
Thanks,
Vicente
--
[ 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 ]
Author: =3D?ISO-8859-1?Q?Daniel_Kr=3DFCgler?=3D <daniel.kruegler@googlemail.c=.om>
Date: Mon, 28 Mar 2011 11:53:19 CST Raw View
On 2011-03-22 20:26, viboes wrote:
[..]
I don't know why I believed that with the introduction of scoped enums
> the single values an enum class can take is one of the enumerators.
> But I see that I was completely wrong and that nothing has been
> changed in this respect since eums where introduce in C.
>
I have never heart of an even stricter enumeration specification as we
currently have in C++0x for scoped enum types. With such a hypothetical
restriction, enums would barely be used as bitmask types as described in
[bitmask.types]. Consider this adapted form for a scoped enumeration type:
enum class bitmask : int_type { V0 = 1 << 0, V1 = 1 << 1 };
constexpr bitmask operator|(bitmask x, bitmask y) {
return static_cast<bitmask>(static_cast<int_type>(x) |
static_cast<int_type>(y));
}
[..]
This would mean that attempting to form the value bitmask::V0|bitmask::V1
would be undefined behaviour, because bitmask::V0|bitmask::V1 does not have
a corresponding enumerator.
It would also mean that the term value-initialization for a scoped enum
would not be defined, which looks really odd to me.
If you need a type with such constraints I suggest that you define instead =
a
class type that does all these validations. Here you can delete the default
constructor as well.
HTH & Greetings from Bremen,
Daniel Kr=FCgler
--
[ 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 ]