C++ Logo


Advanced search

Re: [std-proposals] New features for enum class

From: Baruch Burstein <bmburstein_at_[hidden]>
Date: Sun, 13 Feb 2022 21:01:39 +0200
On Fri, Feb 11, 2022 at 9:43 PM Francesco Pretto via Std-Proposals
<std-proposals_at_[hidden]> wrote:
> Sorry for not commenting your proposals on the merits: from a quick
> fly by I don't see the most important missing feature from C# enums
> that is the bitwise operator enabled for enums with [Flags]
> attribute[1]. Even ObjectiveC has a similar feature (NS_OPTIONS
> enums[2]). Sure, it's possible to add overloads[3] for bitwise
> operators but it would be much more handy if the c++ standard
> supported natively this behavior with a descriptor of sort. I highly
> recommend to add it in your proposal because it would be sad to
> discuss again about enums after years and forgetting this once again.
> Regards,
> Francesco

While on the topic of bitwise operators, I think it would be
convenient to have an option (via attribute or otherwise) to mark an
enum as a bitfield. This would, in addition to enabling bitwise
operators, also change the default values so that each non-explicitly
assigned enum value will be the previous one shifted left, starting
from 1, instead of the current previous one +1, starting from 0. e.g.:

[bitfield] // this would also enable bitwise operations
enum Colors {
  Blue, //implicitly 0x1
  Green, // implicitly 0x02
  Red, // implicitly 0x04
  Cyan = 0x3, // explicit value
  Brown, // implicitly 0x6
  Magenta = 0x5,
  Black = 0x0,
  White = 0x7,

I am curious if anyone else is interested in such a feature. I have
never written a proposal before, and am not sure what the procedure
is, but would like to hear if anyone else would be interested in such
a feature.

Received on 2022-02-13 19:02:15