Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Pedro Alves <palves@redhat.com>
To: gdb-patches@sourceware.org
Subject: Re: [PATCH] Type-safe wrapper for enum flags
Date: Fri, 30 Oct 2015 09:48:00 -0000	[thread overview]
Message-ID: <56326CE9.7010507@redhat.com> (raw)
In-Reply-To: <1446144341-21267-1-git-send-email-palves@redhat.com>

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<foo_values>}’ and ‘bar_values’)
   f |= bar_val1;
     ^

Thanks,
Pedro Alves


  reply	other threads:[~2015-10-29 19:01 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-10-30  8:12 Pedro Alves
2015-10-30  9:48 ` Pedro Alves [this message]
2015-10-30 17:08 ` Simon Marchi
2015-10-30 17:39   ` Pedro Alves
2015-10-31 20:11     ` Simon Marchi
2015-10-31 20:28       ` Pedro Alves
2015-11-01  2:07 ` Doug Evans
2015-11-03 11:48   ` Pedro Alves
2015-11-01  4:04 ` Doug Evans
2015-11-03 12:23   ` Pedro Alves
2015-11-01 15:19 ` Patrick Palka
2015-11-03 11:18   ` Pedro Alves
2015-11-09 13:25     ` Patrick Palka
2015-11-17 13:49       ` Pedro Alves

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=56326CE9.7010507@redhat.com \
    --to=palves@redhat.com \
    --cc=gdb-patches@sourceware.org \
    /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