C++ Logo

std-proposals

Advanced search

Re: [std-proposals] A proposal on default enum initialization

From: Jason McKesson <jmckesson_at_[hidden]>
Date: Fri, 24 Mar 2023 11:06:52 -0400
On Fri, Mar 24, 2023 at 10:32 AM Aleksej Penkov via Std-Proposals
<std-proposals_at_[hidden]> wrote:
>
> Dear team,
>
>
>
> I have a proposal to introduce an optional default-initializer for enum types.
>
> For me it looks like currently an enum variable is by default initialized with a default value of the underlying type.

Enums are scalar types, and "by default" they are initialized like any
other scalar type. Default initialization leaves it uninitialized
(static initialization can initialize it to zero, but otherwise its
value is undefined).

>
>
>
> Code example:
>
> #include <iostream>
>
>
>
> enum TestEnum {
>
> good = -100,
>
> bad = 200
>
> };
>
>
>
> int main()
>
> {
>
> TestEnum h;
>
> std::cout << h << std::endl;
>
> return 0;
>
> }
>
>
>
> Output: 0
>
>
>
> The resulting value 0 is neither of the range ones.
>
> The proposal is to have a way to specify the default one.

What's the motivation for this proposal? Why is this important enough
to deserve a language change?

Received on 2023-03-24 15:07:03