From: Simon Marchi <simon.marchi@polymtl.ca>
To: Pedro Alves <palves@redhat.com>
Cc: Simon Marchi <simon.marchi@ericsson.com>, gdb-patches@sourceware.org
Subject: Re: [PATCH] Default initialize enum flags to 0
Date: Tue, 21 Feb 2017 16:51:00 -0000 [thread overview]
Message-ID: <c33b5b2d1b1e83f6eedc64c9307fdf6a@polymtl.ca> (raw)
In-Reply-To: <80e4049b-0dc1-c396-e788-36e07015c69c@redhat.com>
On 2017-02-21 06:16, Pedro Alves wrote:
> On 02/21/2017 03:01 AM, Simon Marchi wrote:
>
>>
>>> #2 - The other reason is that it's nice IMO to leave enums and enum
>>> flags
>>> easily interchangeable -- i.e., make them behave as close as
>>> possible.
>>> Having one be default initialized, and the other value initialized
>>> means that when changing variables from one type to the other
>>> one needs to consider that aspect.
>>
>> Well, they're not directly interchangeable in C++, which is the whole
>> point of having enum flags.
>
> TBC, by "interchangeable" I meant, when you refactor/redesign code and
> decide the flags would be better as normal enums, and vice versa.
>
> Passing an enum flags to a function expecting a raw enum
> (because it was compiled in C) and vice versa would probably
> not be interchangeable at run time, depending on ABI.
>
>>> #3 - Default initializing to zero can hide bugs that would otherwise
>>> be caught with -Winitialized.
>>
>> (-Wuninitialized?)
>>
>> I don't really understand how this could hide a bug.
>
> I was thinking of the "this code path should have set flags to
> something
> non-zero, but the compiler didn't warn because the variable
> was initialized" kind of bug.
>
>> When we don't
>> initialize the field in the default constructor, does -Wuninitialized
>> issue a warning for this?
>>
>> my_flags flags;
>> flags |= some_flag;
>>
>> I tried quickly and it doesn't seem so. As stated above, if we have
>> the
>> default constructor of the enum flag initialize the value to 0, it
>> won't
>> be a bug in C++, but it will generate a warning in C where plain enums
>> are used.
>
> Bah, I assumed it did! But now that I try, it really doesn't. :-(
>
> I filed a GCC bug now:
>
> [-Wuninitialized] referencing uninitialized field of POD struct should
> warn
> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=79658
Oh so it's only in some very specific cases that the warning is
missing...
> This was my strongest argument, and I'm left without it, so...
TBH, my original motivation was so we could be lazy and leave out the
initializations, I only realized while reading your message that it was
actually a problem :).
>> So if we don't initialize the value to 0 in the default constructor,
>> compiling this code in C++ will be a bug but will not generate any
>> warning. This seems very error prone to me.
>
> Agreed, unfortunately...
>
> Looking at the patch:
>
>> @@ -117,6 +117,7 @@ private:
>> public:
>> /* Allow default construction, just like raw enums. */
>> enum_flags ()
>> + : m_enum_value ((enum_type) 0)
>> {}
>>
>
> The "just like raw enums" comment is no longer true. Please tweak
> that.
>
> OK with that fixed.
Thanks, pushed with that fixed.
prev parent reply other threads:[~2017-02-21 16:51 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-02-20 21:46 Simon Marchi
2017-02-20 23:18 ` Pedro Alves
2017-02-21 3:01 ` Simon Marchi
2017-02-21 11:16 ` Pedro Alves
2017-02-21 16:51 ` Simon Marchi [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=c33b5b2d1b1e83f6eedc64c9307fdf6a@polymtl.ca \
--to=simon.marchi@polymtl.ca \
--cc=gdb-patches@sourceware.org \
--cc=palves@redhat.com \
--cc=simon.marchi@ericsson.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox