From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailman by lists.gnu.org with archive (Exim 4.43) id 1NhSSS-00076q-Hb for mharc-grub-devel@gnu.org; Tue, 16 Feb 2010 13:47:40 -0500 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1NhSSQ-000743-R7 for grub-devel@gnu.org; Tue, 16 Feb 2010 13:47:38 -0500 Received: from [140.186.70.92] (port=48522 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NhSSM-0006x2-P2 for grub-devel@gnu.org; Tue, 16 Feb 2010 13:47:38 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1NhSSL-0003Qc-Ra for grub-devel@gnu.org; Tue, 16 Feb 2010 13:47:34 -0500 Received: from nereid.marlboro.edu ([206.192.68.76]:46162) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1NhSSL-0003QY-LZ for grub-devel@gnu.org; Tue, 16 Feb 2010 13:47:33 -0500 Received: from [192.168.1.103] (71-10-224-192.dhcp.oxfr.ma.charter.com [71.10.224.192]) (Authenticated sender: idupree) by nereid.marlboro.edu (Postfix) with ESMTPSA id 426C22341DA; Tue, 16 Feb 2010 13:47:21 -0500 (EST) Message-ID: <4B7AE847.8010908@isaac.cedarswampstudios.org> Date: Tue, 16 Feb 2010 13:47:35 -0500 From: Isaac Dupree User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.7) Gecko/20100120 Shredder/3.0.1 MIME-Version: 1.0 To: The development of GNU GRUB References: <4B736F82.7010005@gmail.com> <4B742B70.3080303@gmail.com> <4B7A9075.9050400@gmail.com> <4B7ADDF6.3090500@isaac.cedarswampstudios.org> <2e59e6971002161015v55254370y6e3c84cd20126d50@mail.gmail.com> In-Reply-To: <2e59e6971002161015v55254370y6e3c84cd20126d50@mail.gmail.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Marlboro-Information: Please contact techsupport@marlboro.edu for more information X-Marlboro-MailScanner-ID: 426C22341DA.A0946 X-Marlboro-MailScanner: clean X-Marlboro-SpamCheck: not spam, SpamAssassin (not cached, score=-1.94, required 5.5, autolearn=disabled, ALL_TRUSTED -1.80, BAYES_20 -0.74, J_CHICKENPOX_43 0.60) X-Marlboro-MailScanner-From: ml@isaac.cedarswampstudios.org X-Marlboro-MailScanner-Watermark: 1266950842.05564@7itiWy/acg9T5q/DEvR2AQ X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 3) Subject: [Off-topic] C++ enums X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: The development of GNU GRUB List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 16 Feb 2010 18:47:39 -0000 On 02/16/10 13:15, richardvoigt@gmail.com wrote: > On Tue, Feb 16, 2010 at 12:03 PM, Isaac Dupree > wrote: >> On 02/16/10 10:52, Michal Suchanek wrote: >>>> >>>> enum allows it just fine >>> >>> Not here: >>> >>> typedef enum t1 { BTI1 = 1, >> >> typo, should be "BIT1". then it works. (In C. Also remember not to get >> confused by the fact that it doesn't work in C++, for type-related reasons > > Says who? OK, I guess I'll get into it, since you asked... > Comeau is perfectly happy with this code, in strict C++ mode: > > enum flags > { > BIT1 = 1, > BIT2 = 2, > BIT12 = BIT1 | BIT2 > }; In C and C++, enums are their own type distinct from int. In C, there exist implicit conversions both from and to all enum types and int. In C++, there only exist implicit conversions from enum types to int. Bitwise and arithmetic operators only operate on int, not enum types (unless a C++ operator overload is declared); these operators appear to work with enums because of all the implicit conversion. Your example works even in C++ because the right-hand side of an "=" in an enum-declaration is of type int, rather than the enum type (as you can see from the fact that you can put "1" there as well). Now BIT1, BIT2, and BIT12 are all (in C++) values (rvalues) of type "flags" (note this does not apply to the left-hand sides in the enum declaration that define their constant values, but it does apply to those right-hand sides). Now try and combine your with this main function: int main(int argc, char** argv) { flags foo1 = BIT1; // fine flags foo12 = BIT12; // fine int bar = BIT1 | BIT2; // fine flags baz = BIT1 | BIT2; // error return 0 ; } (If you want to see it compile in C, add "typedef enum flags flags;" before main().) See http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.19 for some discussion by a C++ expert. And then return to GRUB2 coding where none of this matters. The only weird thing about enums in C is that they're not guaranteed by the standard to be isomorphic to type "int"; each enum might correspond to a different-size, (possibly even different-signedness), integral type, if this is possible given the range of values in the particular "enum{...};" declaration. (This weird thing also affects C++ but I omitted it in the above explanation of enum versus integral types.) -Isaac