From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 123827 invoked by alias); 21 Feb 2017 11:16:57 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Received: (qmail 123793 invoked by uid 89); 21 Feb 2017 11:16:56 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.9 required=5.0 tests=BAYES_00,RP_MATCHES_RCVD,SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=H*f:sk:d8c3b97, H*i:sk:73ee5ce, H*MI:sk:73ee5ce, H*f:sk:73ee5ce X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 21 Feb 2017 11:16:55 +0000 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 732A361B92; Tue, 21 Feb 2017 11:16:55 +0000 (UTC) Received: from [127.0.0.1] (ovpn04.gateway.prod.ext.ams2.redhat.com [10.39.146.4]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id v1LBGqnY020263; Tue, 21 Feb 2017 06:16:53 -0500 Subject: Re: [PATCH] Default initialize enum flags to 0 To: Simon Marchi References: <20170220214548.18024-1-simon.marchi@ericsson.com> <73ee5ceea586400d0ec017304ce3d3f0@polymtl.ca> Cc: Simon Marchi , gdb-patches@sourceware.org From: Pedro Alves Message-ID: <80e4049b-0dc1-c396-e788-36e07015c69c@redhat.com> Date: Tue, 21 Feb 2017 11:16:00 -0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.4.0 MIME-Version: 1.0 In-Reply-To: <73ee5ceea586400d0ec017304ce3d3f0@polymtl.ca> Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 7bit X-SW-Source: 2017-02/txt/msg00565.txt.bz2 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 This was my strongest argument, and I'm left without it, so... > 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, Pedro Alves