Is there a way to change how an enum sets the values of its constants? Normally it's incrementing by one but I want to apply an other rule. In PAWN this would work
enum (<<=1) {
a = 1,//0b001
b,//0b010
c//0b100
}
Is there a way to do this in C++?
Incrementing an enumeration requires a cast to convert the integer result of addition back to the enumeration type, as in: d = day(d + 1);
No. enum s are not designed to "wrap around" in the way you describe by default.
An ENUM column can have a maximum of 65,535 distinct elements.
Enumeration or Enum in C is a special kind of data type defined by the user. It consists of constant integrals or integers that are given names by a user. The use of enum in C to name the integer values makes the entire program easy to learn, understand, and maintain by the same or even different programmer.
not automatically, but you can specify manually each value
enum X {
a = 0x01,
b = 0x02,
c = 0x04
};
You could automate this shifting process using templates metaprogramming:
template<int by>
struct OneShlBy {
enum { Value = OneShlBy<by - 1>::Value << 1 };
};
template<>
struct OneShlBy<0> {
enum { Value = 1 };
};
enum X {
a = OneShlBy<0>::Value,
b = OneShlBy<1>::Value,
c = OneShlBy<2>::Value
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With