enum-class

C++ enum class: Cast to non existing entry

ⅰ亾dé卋堺 提交于 2021-02-19 04:52:35
问题 I have this situation on one Project where we have some socket-communication that mainly exchanges characters for flow-control. We cast those characters to an enum class : char in a switch. I was wondering, what might happen, if the other end sends an character that is not in our enum class. I have this mwe: enum class Foo : char { UNKNOWN, ENUM1 = 'A', ENUM2 = 'B', ENUM3 = 'C' }; char bar1() { return 'B'; } char bar2() { return 'D'; } int main() { switch((Foo)bar1()) { case Foo::UNKNOWN:std:

why can enum class values of type int not be used as int

|▌冷眼眸甩不掉的悲伤 提交于 2021-02-05 11:25:28
问题 I wanted to change an old-style enum to enum class : int because of its own scope. But the compiler complains about using the values in integer arithmetics. But why - since the enum is explicitly typed as int? example: enum class MsgType : int { Output = 1, Input = 2, Debug = 3 }; enum class MsgSender : int { A = 1, B = 2, C = 3 }; // later in code int id = (MsgType::Input << 16) + (MsgSender::A); produces error C2678: binary '<<': no operator found which takes a left-hand operand of type

why can enum class values of type int not be used as int

纵然是瞬间 提交于 2021-02-05 11:22:07
问题 I wanted to change an old-style enum to enum class : int because of its own scope. But the compiler complains about using the values in integer arithmetics. But why - since the enum is explicitly typed as int? example: enum class MsgType : int { Output = 1, Input = 2, Debug = 3 }; enum class MsgSender : int { A = 1, B = 2, C = 3 }; // later in code int id = (MsgType::Input << 16) + (MsgSender::A); produces error C2678: binary '<<': no operator found which takes a left-hand operand of type

Class vs enum class as an index type

孤者浪人 提交于 2020-06-27 07:23:34
问题 P0138R2 proposal begins with 1 There is an incredibly useful technique for introducing a new integer type that is almost an exact copy, yet distinct type in modern C++11 programs: an enum class with an explicitly specified underlying type. Example: enum class Index : int { }; // Note: no enumerator. One can use Index as a new distinct integer type, it has no implicit conversion to anything (good!). To convert Index to its underlying type it is useful to define int operator*(Index index) {

Using `reinterpret_cast` on an enum class - valid or undefined behavior?

北战南征 提交于 2020-06-17 06:47:51
问题 #include <iostream> #include <cassert> #include <type_traits> template<typename T> using Underlying = std::underlying_type_t<T>; enum class ETest : int { Zero = 0, One = 1, Two = 2 }; template<typename T> auto& castEnum(T& mX) noexcept { // `static_cast` does not compile // return static_cast<Underlying<T>&>(mX); return reinterpret_cast<Underlying<T>&>(mX); } int main() { auto x(ETest::Zero); castEnum(x) = 1; assert(x == ETest::One); return 0; } ideone Is this code guaranteed to always work?

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

北城以北 提交于 2020-01-31 04:04:48
问题 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 ? 回答1: 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

Can enum class be nested?

走远了吗. 提交于 2020-01-22 19:29:08
问题 Can this be done? enum A { enum B { SOMETHING1, SOMETHING2 }; enum C { SOMETHING3, SOMETHING4 }; }; If not is there an alternative solution? The purpose of this question: Want/need to be able to do something like this: enum class ElementaryParticleTypes { enum class MATTER { enum class MESONS { PI }; enum class BARYONS { PROTON, NEUTRON }; enum class LEPTONS { ELECTRON }; }; enum class ANTI_MATTER { enum class ANTI_MESONS { ANTI_PI }; enum class ANTI_BARYONS { ANTI_PROTON ANTI_NEUTRON }; enum

Use 'using enum' in C++20 in classes possible?

怎甘沉沦 提交于 2020-01-14 18:56:10
问题 In this answer it was mentioned that in the upcoming C++20 standard it is possible to use the using statement on enum class and import the enum fields into the local namespace. I was wondering if that also means that I can also use it within class definitions like this: class Foo { enum class Color { red, blue }; using enum Color; }; int main() { Foo::Color c = Foo::red; } Or do I still need to give the full namespace?: Foo::Color c = Foo::Color::red; I tried it in wandbox.org, but it seems

Use 'using enum' in C++20 in classes possible?

浪子不回头ぞ 提交于 2020-01-14 18:54:24
问题 In this answer it was mentioned that in the upcoming C++20 standard it is possible to use the using statement on enum class and import the enum fields into the local namespace. I was wondering if that also means that I can also use it within class definitions like this: class Foo { enum class Color { red, blue }; using enum Color; }; int main() { Foo::Color c = Foo::red; } Or do I still need to give the full namespace?: Foo::Color c = Foo::Color::red; I tried it in wandbox.org, but it seems

User-defined implicit conversion of an enum class when calling an overloaded operator fails

∥☆過路亽.° 提交于 2020-01-02 04:03:14
问题 Consider the following example: struct ConvertibleStruct {}; enum class ConvertibleEC {}; struct Target { // Implicit conversion constructors Target(ConvertibleStruct) {} Target(ConvertibleEC) {} }; Target operator~(const Target& t) { return t; } Target anotherFunction(const Target& t) { return t; } int main() { ConvertibleStruct t; ConvertibleEC ec; ~t; // 1. Works finding the operator overloaded above ~ec; // 2. Fails to compile on clang 3.4 and gcc 4.8.2 operator~(ec); // 3. Works finding