1

I have the following Enum in CLR / CLI:

public enum class Days
{
    Sunday,
    Monday,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday
};

In C#, if I wanted to create a combination of the selected enums, I used to add [Flags] attribute before the declaration of the enum.

Does anything similar to that exist in C++ CLR?

Matthieu
  • 4,605
  • 4
  • 40
  • 60
Nave Tseva
  • 371
  • 2
  • 6
  • 16
  • You don't need the Flags attribute. You just need to define the enumeration values appropriately so that AND, OR, NOT and XOR bitwise operations can be performed on them, i.e. assign each value the next greater power of 2. – mm8 Sep 05 '18 at 14:40
  • @mm8 Thanks! Is it doable without specifying Enum values explicitly? – Nave Tseva Sep 05 '18 at 14:42
  • No, it's not. You need to define the values explicitly. – mm8 Sep 05 '18 at 14:47

2 Answers2

1

The FlagsAttribute in C# just indicates that the enumeration can be treated as a bit field.

What really matters is that you define the enumeration values appropriately so that AND, OR, NOT and XOR bitwise operations can be performed on them, i.e. you should assign each enumeration value the next greater power of 2:

public enum class Days
{
    Sunday = 1,
    Monday = 2,
    Tuesday = 4,
    Wednesday = 8,
    Thursday = 16,
    Friday = 32,
    Saturday = 64
};

[Flags] does not automatically make the enum values powers of two.

What does the [Flags] Enum Attribute mean in C#?

mm8
  • 163,881
  • 10
  • 57
  • 88
1

You can use the flags attribute in C++/CLI like this:

[System::Flags]
public enum class Days : int
{
    Sunday = 1,
    Monday = 2,
    Tuesday = 4,
    Wednesday = 8,
    Thursday = 16,
    Friday = 32,
    Saturday = 64
};

[Flags] does not automatically make the enum values powers of two. But it can be required by some static code analysis tools:

PVS Studio

Sonar Lint

Matthieu
  • 4,605
  • 4
  • 40
  • 60