enum-class

c++: enum inside of a class using “enum class”

五迷三道 提交于 2019-12-08 02:39:42
问题 What would be the right way to write an enum inside a class? I am writing conway's game of life code in c++. So i have a patterns class which contains the info about different kind of patterns: class Patterns { public: Patterns(); ~Patterns(void); private: enum class PatternType { bloat, block, blinker, toad }; PatternType pattern_; }; My goal is not to pollute the global space here. So is it the write way of writing the enum inside of a class keeping oops concepts in mind. If not, please

Is it possible to make a scoped enumeration (“enum class”) contextually convertible to bool?

喜你入骨 提交于 2019-12-07 02:15:18
问题 Let's say I have enum class Flags : std::uint16_t { None = 0, A = 0x0001, B = 0x0002, C = 0x0004 } inline Flags operator|(Flags lhs, Flags rhs) { return static_cast<Flags>(static_cast<std::uint16_t>(lhs) | static_cast<std::uint16_t>(rhs)); } inline Flags operator&(Flags lhs, Flags rhs) { return static_cast<Flags>(static_cast<std::uint16_t>(lhs) & static_cast<std::uint16_t>(rhs)); } inline Flags operator|=(Flags& lhs, Flags rhs) { return lhs = lhs | rhs; } inline Flags operator&=(Flags& lhs,

c++: enum inside of a class using “enum class”

空扰寡人 提交于 2019-12-06 07:48:40
What would be the right way to write an enum inside a class? I am writing conway's game of life code in c++. So i have a patterns class which contains the info about different kind of patterns: class Patterns { public: Patterns(); ~Patterns(void); private: enum class PatternType { bloat, block, blinker, toad }; PatternType pattern_; }; My goal is not to pollute the global space here. So is it the write way of writing the enum inside of a class keeping oops concepts in mind. If not, please suggest some better alternatives and what are their benefits over this method. Any inputs on keeping

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

别说谁变了你拦得住时间么 提交于 2019-12-05 10:48:57
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 the operator overloaded above anotherFunction(ec); // 4. Works } Compiler versions: The above findings

Is it safe to reinterpret_cast an enum class variable to a reference of the underlying type?

人盡茶涼 提交于 2019-12-03 19:26:58
问题 I've seen reinterpret_cast used to apply incrementation to enum classes, and I'd like to know if this usage is acceptable in standard C++. enum class Foo : int8_t { Bar1, Bar2, Bar3, Bar4, First = Bar1, Last = Bar4 }; for (Foo foo = Foo::First; foo <= Foo::Last; ++reinterpret_cast<int8_t &>(foo)) { ... } I know casting to a reference of a base class is safe in case of trivial classes. But since enum classes are not event implicitly converted to their underlying types, I'm not sure if and how

User Defined C++11 enum class Default Constructor

孤者浪人 提交于 2019-12-03 08:12:58
问题 Is there a way to specify the default constructor of an enum class ? I am using an enum class to specify a set of values which are allowable for a particular datatype in a library: in this case, it's the GPIO pin id numbers of a Raspberry Pi. It looks something like this: enum class PinID : int {N4 = 4, N17 = 17, /* ...etc... */ } The point of me doing this instead just of using, say, an int is to ensure that code is safe: I can static_assert (or otherwise compile-time ensure -- the actual

Is it possible to determine the number of elements of a c++ enum class?

不羁岁月 提交于 2019-12-03 03:23:36
问题 Is it possible to determine the cardinality of a c++ enum class : enum class Example { A, B, C, D, E }; I tried to use sizeof , however, it returns the size of an enum element. sizeof(Example); // Returns 4 (on my architecture) Is there a standard way to get the cardinality (5 in my example) ? 回答1: Not directly, but you could use the following trick: enum class Example { A, B, C, D, E, Count }; Then the cardinality is available as (int)Example::Count . Of course, this only works nicely if you

C++11 standard conformant bitmasks using enum class

偶尔善良 提交于 2019-12-03 03:08:01
问题 Can you implement standard conformant (as described in 17.5.2.1.3 of the n3242 draft) type safe bitmasks using enum class? The way I read it, a type T is a bitmask if it supports the |,&,^,~,|=,&= and ^= operators and further you can do if(l&r) where l and r are of type T. Missing from the list are the operator != and == and to allow sorting one probably also wants to overload <. Getting the operators to works is just annoying boilerplate code but I do not see how to do if(l&r). At least the

User Defined C++11 enum class Default Constructor

笑着哭i 提交于 2019-12-02 21:51:52
Is there a way to specify the default constructor of an enum class ? I am using an enum class to specify a set of values which are allowable for a particular datatype in a library: in this case, it's the GPIO pin id numbers of a Raspberry Pi. It looks something like this: enum class PinID : int {N4 = 4, N17 = 17, /* ...etc... */ } The point of me doing this instead just of using, say, an int is to ensure that code is safe: I can static_assert (or otherwise compile-time ensure -- the actual method used is not important to me) things like that someone hasn't made a spelling error (passing a 5

C++11 standard conformant bitmasks using enum class

橙三吉。 提交于 2019-12-02 17:40:13
Can you implement standard conformant (as described in 17.5.2.1.3 of the n3242 draft) type safe bitmasks using enum class? The way I read it, a type T is a bitmask if it supports the |,&,^,~,|=,&= and ^= operators and further you can do if(l&r) where l and r are of type T. Missing from the list are the operator != and == and to allow sorting one probably also wants to overload <. Getting the operators to works is just annoying boilerplate code but I do not see how to do if(l&r). At least the following does not compile with GCC (besides being extremely dangerous as it allows an erroneous