Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to manually define a conversion for an enum class?

Normally you can define a cast for a class by using the following syntax:

class Test { public:   explicit operator bool() { return false; } }; 

Is there a way to do this or something similar for an enum class?

like image 365
OmnipotentEntity Avatar asked Oct 05 '12 20:10

OmnipotentEntity


People also ask

Can you define an enum within a class?

Yes, we can define an enumeration inside a class. You can retrieve the values in an enumeration using the values() method.

Can enum class be derived?

Not possible. There is no inheritance with enums. You can instead use classes with named const ints.

Can we convert enum to string C++?

The stringify() macro method is used to convert an enum into a string. Variable dereferencing and macro replacements are not necessary with this method. The important thing is that, only the text included in parenthesis may be converted using the stringify() method.

What is the difference between a class enum and a regular enum?

An enum can, just like a class , have attributes and methods. The only difference is that enum constants are public , static and final (unchangeable - cannot be overridden). An enum cannot be used to create objects, and it cannot extend other classes (but it can implement interfaces).


2 Answers

No, it's not.

Actually, an enum class is no class at all. The class keyword is only used because suddenly changing the unscoped enum to a scoped enum would have mean reworking all enums codes. So the committee decided that to distinguish between new-style and old-style enums, the new ones would be tagged with class, because it's a keyword already so no enum could have been named class in C++. They could have picked another, it would not have made much more sense anyway.

However, despite the class keyword they are still regular enums in that only enumerators (and potentially values assigned to them) are allowed within the brackets.

like image 175
Matthieu M. Avatar answered Sep 22 '22 15:09

Matthieu M.


No, but you can make a normal class type act like an enum class, using constexpr members and constructors. And then you can add all the additional member functions you want.


Proof that it can work even with switch:

#include <iostream>  struct FakeEnum {     int x;      constexpr FakeEnum(int y = 0) : x(y) {}      constexpr operator int() const { return x; }      static const FakeEnum A, B, Z; };  constexpr const FakeEnum FakeEnum::A{1}, FakeEnum::B{2}, FakeEnum::Z{26};  std::istream& operator>>(std::istream& st, FakeEnum& fe) {     int val;     st >> val;     fe = FakeEnum{val};     return st; }  int main() {     std::cout << "Hello, world!\n";     FakeEnum fe;     std::cin >> fe;      switch (fe)     {         case FakeEnum::A:         std::cout << "A\n";         break;         case FakeEnum::B:         std::cout << "B\n";         break;         case FakeEnum::Z:         std::cout << "Z\n";         break;     } } 

Proof that working with switch does not require implicit interconversion with int:

#include <iostream>  /* pseudo-enum compatible with switch and not implicitly convertible to integral type */ struct FakeEnum {     enum class Values { A = 1, B = 2, Z = 26 };     Values x;      explicit constexpr FakeEnum(int y = 0) : FakeEnum{static_cast<Values>(y)} {}     constexpr FakeEnum(Values y) : x(y) {}      constexpr operator Values() const { return x; }     explicit constexpr operator bool() const { return x == Values::Z; }      static const FakeEnum A, B, Z; };  constexpr const FakeEnum FakeEnum::A{Values::A}, FakeEnum::B{Values::B}, FakeEnum::Z{Values::Z};  std::istream& operator>>(std::istream& st, FakeEnum& fe) {     int val;     st >> val;     fe = FakeEnum(val);     return st; }  int main() {     std::cout << "Hello, world!\n";     FakeEnum fe;     std::cin >> fe;      switch (fe)     {         case FakeEnum::A:         std::cout << "A\n";         break;         case FakeEnum::B:         std::cout << "B\n";         break;         case FakeEnum::Z:         std::cout << "Z\n";         break;     }     // THIS ERRORS: int z = fe; } 
like image 39
Ben Voigt Avatar answered Sep 20 '22 15:09

Ben Voigt