From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 74942 invoked by alias); 29 Oct 2015 19:01:11 -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 74919 invoked by uid 89); 29 Oct 2015 19:01:10 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.9 required=5.0 tests=AWL,BAYES_00,RP_MATCHES_RCVD,SPF_HELO_PASS autolearn=ham version=3.3.2 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 (AES256-GCM-SHA384 encrypted) ESMTPS; Thu, 29 Oct 2015 19:01:10 +0000 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) by mx1.redhat.com (Postfix) with ESMTPS id 11DC6A80 for ; Thu, 29 Oct 2015 19:01:09 +0000 (UTC) Received: from [127.0.0.1] (ovpn01.gateway.prod.ext.ams2.redhat.com [10.39.146.11]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id t9TJ0xGN015428 for ; Thu, 29 Oct 2015 15:01:04 -0400 Message-ID: <56326CE9.7010507@redhat.com> Date: Fri, 30 Oct 2015 09:48:00 -0000 From: Pedro Alves User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.7.0 MIME-Version: 1.0 To: gdb-patches@sourceware.org Subject: Re: [PATCH] Type-safe wrapper for enum flags References: <1446144341-21267-1-git-send-email-palves@redhat.com> In-Reply-To: <1446144341-21267-1-git-send-email-palves@redhat.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-SW-Source: 2015-10/txt/msg00740.txt.bz2 On 10/29/2015 06:45 PM, Pedro Alves wrote: > > enum foo_flags flags = 0; > > if (...) > flags |= FOO_FLAG1; > if (...) > flags |= FOO_FLAG2; > > ... would have to be written as: > > enum foo_flags flags = (enum foo_flags) 0; > > if (...) > flags = (enum foo_flags) (flags | FOO_FLAG1); > if (...) > flags = (enum foo_flags) (flags | FOO_FLAG2); > > which is ... ugly. Alternatively, we'd have to use an int for the > variable's type, which isn't ideal either. ... for losing type safeness. Looks like I forgot to mention the "type-safe" part. This thus also avoids mistakes like: enum foo { foo_val1, foo_val2 }; enum bar { bar_val1, bar_val2 }; enum foo f = 0; f |= bar_val2; While the above compiles in C, the below doesn't, in C++: enum foo_values { foo_val1, foo_val2 }; enum bar_values { bar_val1, bar_val2 }; DEF_ENUM_FLAGS_TYPE (enum foo_values, foo); DEF_ENUM_FLAGS_TYPE (enum bar_values, bar); foo f = 0; f |= bar_val1; with: $ g++ ... foo.c:30:5: error: no match for ‘operator|=’ (operand types are ‘foo {aka enum_flags}’ and ‘bar_values’) f |= bar_val1; ^ Thanks, Pedro Alves