enum-class

std::get using enum class as template argument

巧了我就是萌 提交于 2019-11-30 03:03:09
问题 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

Forward Declaring enum class not working

丶灬走出姿态 提交于 2019-11-30 01:43:51
问题 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,

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

◇◆丶佛笑我妖孽 提交于 2019-11-29 18:47:25
问题 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? 回答1: 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).

Wrap enum class for cython

霸气de小男生 提交于 2019-11-29 18:19:08
问题 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? 回答1: 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,

Can't use enum class as unordered_map key

自作多情 提交于 2019-11-28 16:47:24
I have a class containing an enum class. class Shader { public: enum class Type { Vertex = GL_VERTEX_SHADER, Geometry = GL_GEOMETRY_SHADER, Fragment = GL_FRAGMENT_SHADER }; //... Then, when I implement the following code in another class... std::unordered_map<Shader::Type, Shader> shaders; ...I get a compile error. ...usr/lib/c++/v1/type_traits:770:38: Implicit instantiation of undefined template 'std::__1::hash<Shader::Type>' What is causing the error here? I use a functor object to calculate hash of enum class : struct EnumClassHash { template <typename T> std::size_t operator()(T t) const {

Implementation of operators for enum class

两盒软妹~` 提交于 2019-11-27 18:45:27
Following the discussion in question Incrementation and decrementation of “enum class” , I'd like to ask about the possible implementation of arithmetic operators for enum class types. Example from the original question: enum class Colors { Black, Blue, White, END_OF_LIST }; // Special behavior for ++Colors Colors& operator++( Colors &c ) { c = static_cast<Colors>( static_cast<int>(c) + 1 ); if ( c == Colors::END_OF_LIST ) c = Colors::Black; return c; } Is there a way to implement arithmetic operators without casting to a type with already defined operators? I can't think of any, but casting

Can't use enum class as unordered_map key

主宰稳场 提交于 2019-11-27 09:49:09
问题 I have a class containing an enum class. class Shader { public: enum class Type { Vertex = GL_VERTEX_SHADER, Geometry = GL_GEOMETRY_SHADER, Fragment = GL_FRAGMENT_SHADER }; //... Then, when I implement the following code in another class... std::unordered_map<Shader::Type, Shader> shaders; ...I get a compile error. ...usr/lib/c++/v1/type_traits:770:38: Implicit instantiation of undefined template 'std::__1::hash<Shader::Type>' What is causing the error here? 回答1: I use a functor object to

How can I output the value of an enum class in C++11

邮差的信 提交于 2019-11-27 06:31:30
How can I output the value of an enum class in C++11? In C++03 it's like this: #include <iostream> using namespace std; enum A { a = 1, b = 69, c= 666 }; int main () { A a = A::c; cout << a << endl; } in c++0x this code doesn't compile #include <iostream> using namespace std; enum class A { a = 1, b = 69, c= 666 }; int main () { A a = A::c; cout << a << endl; } prog.cpp:13:11: error: cannot bind 'std::ostream' lvalue to 'std::basic_ostream<char>&&' /usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/ostream:579:5: error: initializing argument 1 of 'std::basic_ostream<_CharT,

Implementation of operators for enum class

你。 提交于 2019-11-26 19:43:18
问题 Following the discussion in question Incrementation and decrementation of “enum class”, I'd like to ask about the possible implementation of arithmetic operators for enum class types. Example from the original question: enum class Colors { Black, Blue, White, END_OF_LIST }; // Special behavior for ++Colors Colors& operator++( Colors &c ) { c = static_cast<Colors>( static_cast<int>(c) + 1 ); if ( c == Colors::END_OF_LIST ) c = Colors::Black; return c; } Is there a way to implement arithmetic

How can I output the value of an enum class in C++11

只谈情不闲聊 提交于 2019-11-26 12:05:29
问题 How can I output the value of an enum class in C++11? In C++03 it\'s like this: #include <iostream> using namespace std; enum A { a = 1, b = 69, c= 666 }; int main () { A a = A::c; cout << a << endl; } in c++0x this code doesn\'t compile #include <iostream> using namespace std; enum class A { a = 1, b = 69, c= 666 }; int main () { A a = A::c; cout << a << endl; } prog.cpp:13:11: error: cannot bind \'std::ostream\' lvalue to \'std::basic_ostream<char>&&\' /usr/lib/gcc/i686-pc-linux-gnu/4.5.1/.