191 questions
5
votes
2
answers
2k
views
How to get enum from boost::property_tree?
How do I get an enum from a boost::property_tree?
This is my "non-working" example.
config.xml
<root>
<fooEnum>EMISSION::EMIT1</fooEnum>
<fooDouble>42</fooDouble>
...
1
vote
1
answer
800
views
enum input from xml to c++ program using boost::property_tree
How do you read a enum-class from an XML file using the boost::property_tree library?
I would like to avoid reading it as a string and mapping the string to the enum-class in my program.
59
votes
1
answer
19k
views
Elaborated-type-specifier for a scoped enum must not use the ‘class’ keyword
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 ...
2
votes
3
answers
1k
views
enum class in c++ can not compile in Mac, while works in Windows
it seems there are some problems with my g++ 4.2 compiler in my Mac. I defined a enum class as follows.
enum class Suit {CLUBS,SPADES,HEARTS,DIAMONDS};
It can compile and run in VS2013 but failed to ...
19
votes
3
answers
4k
views
Is it safe to reinterpret_cast an enum class variable to a reference of the underlying type?
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,
...
21
votes
1
answer
30k
views
Forward Declaring enum class not working
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 ...
98
votes
8
answers
69k
views
Can't use enum class as unordered_map key
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
...
10
votes
1
answer
4k
views
How to make enum class to work with the 'bit-or' feature?
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?
2
votes
1
answer
1k
views
Wrap legacy c enum to c++11 enum class
I have an old C library and I have to wrap it inside a C++ environment. I use C++11 enum class because they're so useful and I'd like to do transform the original enum into a new enum class without ...
768
votes
9
answers
553k
views
Why is enum class considered safer to use than plain enum?
I heard a few people recommending to use enum classes in C++ because of their type safety.
But what does that really mean?
5
votes
2
answers
6k
views
typedef and enum or enum class
I have an enum like this: (Actually, it's an enum class)
enum class truth_enum {
my_true = 1,
my_false = 0
};
I would like to be able to expose my_true to the global namespace, so that I can ...
47
votes
3
answers
52k
views
User Defined C++11 enum class Default Constructor
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 ...
11
votes
2
answers
6k
views
Enum class C++11 by reference or value
I have basically two questions may be they are related so I'll put them into one.
Should we pass enum class in C++11 by reference or value when passing to function. It is sort of inheriting primitive ...
9
votes
1
answer
9k
views
Using enum class with std::bitset
First of all I want a normal enumeration instead of a bit-based enumeration, because the amount of different enums will be beyond any integral type. I also want to take advantage of the type safety of ...
0
votes
2
answers
469
views
Sequence of enumerators at compile time
Given a C++11 enum class, is there some templating or other construct to iterate, at compile-time, over the set of all enumerators? Could one define a template to e.g. initialize an array with all ...
6
votes
1
answer
3k
views
The difference between enum and enum class?
I've recently started working the C++/CLI managed code, but I've always defined enums like so:
enum FV_MODE
{
IDLE,DRAG,ADD_HITBOX,ADD_HURTBOX
};
Until today, when I was hit with the error ...
1
vote
0
answers
347
views
c++11: Type transformation of an enum-class-type derivative via alias-template
Consider the following code, which compiles fine in clang but not in gcc (4.7.2):
template<typename T> using remove_ref_typed =
typename std::remove_reference<T>::type; // alias-...
44
votes
3
answers
117k
views
Implementation of operators for enum class
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 ...
5
votes
1
answer
2k
views
Static import in C++11 (e.g. an enum class)
My usage of enum class (VS2012):
class matrix {
public:
enum class operation_type {ADD, MULT};
matrix(operation_type op);
...
}
and in another fragment I use
matrix* m = new matrix(matrix::...
8
votes
6
answers
24k
views
Can enum class be nested?
Can this be done?
enum A
{
enum B
{
SOMETHING1,
SOMETHING2
};
enum C
{
SOMETHING3,
SOMETHING4
};
};
If not is there an alternative solution?
...
134
votes
19
answers
126k
views
Is it possible to determine the number of elements of a c++ enum class?
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); // ...
16
votes
2
answers
29k
views
Enum Class "could not convert to unsigned int"
I have an enum class like this:
typedef unsigned int binary_instructions_t;
enum class BinaryInstructions : binary_instructions_t
{
END_INSTRUCTION = 0x0,
RESET,
...
5
votes
2
answers
265
views
Misunderstanding of range-based for loop?
A compiler error occurs when I try to compile the following code:
for(binary_instructions_t &inst: BinaryInstructions){
}
BinaryInstructions is this enum class:
typedef unsigned int ...
1
vote
1
answer
216
views
How is enum class similar to enum, or a class? [duplicate]
Is the type enum class a completely separate from a traditional class, or is its implementation similar? How does enum class work? The reason I ask is because I don't understand how it can be similar ...
4
votes
2
answers
3k
views
C++11, enum class, undefined reference with g++, works with clang++
I used the new C++11 "enum class" type and observed a "undefined reference" problem when using g++. This probleme does not happen with clang++. I do not know if I am doing something wrong or if it is ...
1
vote
2
answers
370
views
Assigning values after defining an enumeration
I'm a newbie to C++ and especially C++11, so since I've now got to use it, a few questions about 'enum' and 'enum class' came up:
Can I assign values after the enumeration has been declared?
enum ...
5
votes
2
answers
2k
views
C++11 enum with class members and constexpr link-time optimization
In my project I have a lot of enumerations that need to have additional attributes associated with the enumeration members and auxiliary static methods associated with the enumeration type.
As much ...
8
votes
4
answers
6k
views
What's an enum class and why should I care?
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 ...
2
votes
2
answers
759
views
Extended enum class
I had enum class, say
enum class Enum{
var1, var2;
}
Now I want to add some member which depends on parameter i.e var3(int). OK, It's not for enum, so I want to change it by regular class, but my ...
1
vote
0
answers
23
views
How can define a ponctuation in an enum?
I use Stanford POStagger and I want to define an enum as bellow :
public enum POSType {
CC("Conjunction"),
CD("Number"),
! ("Punctuation")
......
}
How can I define a ...
4
votes
1
answer
3k
views
C++11 enum class instantiation
I've encountered the following form of enum class variable instantiation and it is compiling without any warning or error under VS2012:
UINT32 id;
enum class X {apple, pear, orange};
X myX = X(id);
...
57
votes
3
answers
23k
views
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 ...
-1
votes
1
answer
172
views
declaring the underlying type of enumeration identifier [closed]
I have declared an enum as follows:
enum fileType {typeA, typeB};
This is causing an error when I try to append directoryType type to a string.
I believe I need to include the underlying type of the ...
43
votes
5
answers
39k
views
C++11 standard conformant bitmasks using enum class
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 |,&,^,~,|=,&...
4
votes
1
answer
256
views
Is self-documenting code worth potential performance issues?
I created small class that allows me to use enumerators of strongly-typed enums as flags (in combination). I'm using type_traits for underlying type detection so it should be also slightly type safe ...
148
votes
10
answers
214k
views
How can I output the value of an enum class in C++11
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;
...
14
votes
7
answers
13k
views
std::get using enum class as template argument
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;
/* ... */
...
7
votes
2
answers
979
views
Link compatibility of enums and enum classes
Suppose there is a C++11 API that uses enum classes:
// api.hpp
enum class E {A, B, C};
void f(E);
...
// api.cpp
void f(E e)
{
if (e == E::A)
...
}
Now suppose I would like to use this ...
9
votes
3
answers
5k
views
validate integer is some enum class item (C++11)
i have some enum class
enum class Foo { A=1, B=18 , Z=42 };
i want to check if some integer can be converted into a Foo.
What would be the ideal way to do this? this is for runtime check (the ...
9
votes
3
answers
934
views
Can I use enum class values as arguments to varargs functions?
C++11 adds enum classes, which are stronger-typed enums - values of enum classes will not be implicitly converted to values of other enum classes or integers, and forward-declarations are permitted by ...
8
votes
1
answer
3k
views
strongly typed C++0x enumeration comparison
why aren't instances of strongly typed C++0x enumerations comparable to each other?
Update: They are comparable in gcc 4.6; I'm not sure if it worked in gcc 4.4.