enum-class

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

不想你离开。 提交于 2019-12-02 16:55:39
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) ? 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 let values of the enum be automatically assigned, starting from 0. If that's not the case, you can manually

Elaborated-type-specifier for a scoped enum must not use the ‘class’ keyword

不羁岁月 提交于 2019-12-01 14:56:00
I have the following enum specification: enum class FaceDirection : int8 { Down, Up }; g++ 4.8.1 gives the following error: warning: elaborated-type-specifier for a scoped enum must not use the ‘class’ keyword What causes this? Check the type you are deriving the enum class from exists. In this case, there was no typedef specified for int8 . 来源: https://stackoverflow.com/questions/20459120/elaborated-type-specifier-for-a-scoped-enum-must-not-use-the-class-keyword

Elaborated-type-specifier for a scoped enum must not use the ‘class’ keyword

送分小仙女□ 提交于 2019-12-01 13:41:10
问题 I have the following enum specification: enum class FaceDirection : int8 { Down, Up }; g++ 4.8.1 gives the following error: warning: elaborated-type-specifier for a scoped enum must not use the ‘class’ keyword What causes this? 回答1: Check the type you are deriving the enum class from exists. In this case, there was no typedef specified for int8 . 来源: https://stackoverflow.com/questions/20459120/elaborated-type-specifier-for-a-scoped-enum-must-not-use-the-class-keyword

What's an enum class and why should I care?

点点圈 提交于 2019-12-01 04:36:30
For one who has never written a line of C++11, and who has, at the moment, no opportunity to program in C++11, can you, in one short paragraph., tell me: What is an "enum class" and why do we need it? enum class is called a scoped enumeration . It prevents polluting the namespace where the enumeration appears with the names of the enumerators. In C++03, you could do effectively the same thing by putting the enum inside a dedicated class . Perhaps that's the source of the syntax, which is a bit confusing. Another difference is that the enumerators of such a type don't convert implicitly to int

What's an enum class and why should I care?

◇◆丶佛笑我妖孽 提交于 2019-12-01 02:27:25
问题 For one who has never written a line of C++11, and who has, at the moment, no opportunity to program in C++11, can you, in one short paragraph., tell me: What is an "enum class" and why do we need it? 回答1: enum class is called a scoped enumeration . It prevents polluting the namespace where the enumeration appears with the names of the enumerators. In C++03, you could do effectively the same thing by putting the enum inside a dedicated class . Perhaps that's the source of the syntax, which is

std::get using enum class as template argument

南笙酒味 提交于 2019-11-30 18:06:21
I'm using a std::tuple and defined a class enum to somehow "naming" each of the tuple's fields, forgetting about their actual indexes. So instead of doing this: std::tuple<A,B> tup; /* ... */ std::get<0>(tup) = bleh; // was it 0, or 1? I did this: enum class Something { MY_INDEX_NAME = 0, OTHER_INDEX_NAME }; std::tuple<A,B> tup; /* ... */ std::get<Something::MY_INDEX_NAME> = 0; // I don't mind the actual index... The problem is that, as this compiled using gcc 4.5.2, I've now installed the 4.6.1 version, and my project failed to compile. This snippet reproduces the error: #include <tuple>

Forward Declaring enum class not working

ぃ、小莉子 提交于 2019-11-30 17:53:08
In State.h I have enum class StateID : unsigned int; In State.cpp I have enum class StateID : unsigned int { NullID = 0, MainMenuID, GamePlayID, }; The problem is that any class that includes State.h has the forward declaration, but I can't use any enum value within any cpp file except States.cpp ( which defined it ), like StateID::MainMenuID . The error says... /home/lee/Projects/SuddenAwakening/Source/Game.cpp:24: error: 'MainMenuID' is not a member of 'StateID' I'm running LinuxMint15KDE, g++ 4.7, and I am using c++11 features in other parts like nullptr, unique_ptr, ect..., so it's not

Wrap enum class for cython

好久不见. 提交于 2019-11-30 13:08:36
I am trying to wrap an enum class in a c++ header file for use in a cython project.I have googled around and can not find out how to achieve this - is it supported? CPP class enum class Color {red, green = 20, blue}; Definition of type cdef extern from "colors.h": cdef cppclass Color: pass Definition of color types cdef extern from "colors.h" namespace "Color": cdef Color red cdef Color green cdef Color blue Python implementation cdef class PyColor: cdef Color thisobj def __cinit__(self, int val): self.thisobj = <Color> val def get_color_type(self): cdef c = {<int>red : "red", <int> green :

How to make enum class to work with the 'bit-or' feature?

时光毁灭记忆、已成空白 提交于 2019-11-30 13:00:07
I usually use enum with the 'bit-or' or | together to allow an object has some options. How to make enum class to work with the 'bit-or' feature? You need to overload the operators for your enum class and implement them by casting to the underlying type: enum class foo : unsigned { bar = 1, baz = 2 }; foo operator |(foo a, foo b) { return static_cast<foo>(static_cast<unsigned>(a) | static_cast<unsigned>(b)); } … of course this could be generalised (using SFINAE and std::underlying_type ). That C++ doesn’t provide this out of the box is an oversight, in my opinion. Here’s how a general

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

左心房为你撑大大i 提交于 2019-11-30 08:49:47
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 the code above would be guaranteed to work in all compilers. Any clues? You might want to overload