191 questions
4
votes
1
answer
2k
views
c++11: enum member initialization
I have a class defined as follows
struct X {
X() : data() {}
int data;
enum class Zzz : int { zero, one, two };
Zzz zzz;
};
...
X xval;
What is the value of xval.zzz - is undefined ...
23
votes
6
answers
21k
views
C++ - Is it better to pass an enum class as value or const reference?
Since it's called a 'class' I would usually pass it as const reference, but if I use a plain enum it makes no difference, right? So does it make a difference if I pass an enum class as value or as ...
0
votes
0
answers
341
views
C++ Hide enum class from rest of code
I have some code with an enum class in it. I only want there to exist a single instance of this enum class. I am aware of singleton classes, but is there a way to make an enum class only visible from ...
7
votes
2
answers
1k
views
Initialize a two-dimensional std::array of type enum class (C++11)
I have the following class in C++11:
class MyTable
{
public:
enum class EntryType
{
USED, FREE
};
MyTable(EntryType value)
{
for (uint32_t i = 0; i < 10; ++...
22
votes
3
answers
49k
views
enum class of type string in C++
- Background Information:
There is a class in C++11 known as enum class which you can store variables inside. However, I have only seen the type of the class to be char:
enum class : char {
v1 = '...
0
votes
2
answers
993
views
Singleton (Traditional vs Enum)
Disclaimer: Apologies if this question is too basic.
I'm learning about Singleton and have a quick question its implementation, are these differences purely coding preferences or am I missing ...
1
vote
1
answer
73
views
Implicit convertion of custom class to enum classes
I want to create a class, that will be implicitly convertible to selected enum classes, in order to switch over it. But following code does not compile (gcc). Compiler complains that conversion is ...
0
votes
1
answer
818
views
How to use bitmask_operators.hpp with namespace and classes
I want to use C++11 enum class as bitfields and find a nice approach here.
But I stuck, if my enum class declaration is not in global namespace but in custom namespace or inside of a class instead. E....
3
votes
2
answers
15k
views
How to std::map<enum class, std::string>?
I am trying to std::map, with an enum class and a std::string but I am getting some error.
I am using gcc 4.4.7 with -std=c++0x (this is fixed)
At .h file:
enum class state_t{
unknown,
off,
...
-1
votes
1
answer
403
views
Is GCC correct to warn of format string mismatch with a scoped enum?
When using a scoped enum in a varargs context, it is defined to be passed as its underlying type, as answered in "Can I use enum class values as arguments to varargs functions?" As I understand it, ...
8
votes
2
answers
1k
views
Why can a lambda expression return a local enum class type?
Why and how does this work?
What type is 'auto' here?
auto lambda = [](){
enum class Local { X=0 };
return Local::X;
};
auto x = lambda(); // No error! Why and what type is auto in this ...
4
votes
1
answer
4k
views
QML component enum class property
I have delegate component in a separate qml file, in which I'd like to have a property which is an enum class type coming from a c++ QObject.
Is this possible?
Here is a Minimum (non)Working Example:
...
0
votes
0
answers
70
views
Is it possible to declare an enum which is part of a class in a separate place?
I have a class with an attribute type of a custom enum class Class::Type. Currently the declaration of Class looks like this:
class Class {
enum class Type {
a,
b,
c,
...
0
votes
1
answer
144
views
I'm using :: operator but I still get error that enum class error is not a class, namespace, or scoped enum
I want to print my Car object's color and noise. I'm trying to use c++11's enum class within my Car class. When I compile I get error Car::Color and Car::Noise is not a class, namespace, or scoped ...
2
votes
1
answer
3k
views
Unhandled enum class value in switch() - Exception or Assert?
lvl is an enum class.
switch(lvl)
{
case LogLevel::Trace:
return "Trace";
case LogLevel::Debug:
return "Debug";
case LogLevel::Info:
return "Info";
case LogLevel::Warning:
return "...
3
votes
1
answer
4k
views
enum class and boost serialization [duplicate]
How can I serialize/deserialize a class that has a member of enum class using boost serialization. Example:
enum class enum_class{
item1=0,item2=1
}
class foo{
private:
friend class boost::...
0
votes
2
answers
982
views
C++14 operator << for stream insertion and enum class
I am writing a game in which there are 2 players, "BLACK" and "WHITE".
I have the following enum class:
enum class PlayerType
{
BLACK, WHITE
};
I would like to write an ostream operator<< ...
1
vote
1
answer
407
views
Boost test fails with enum classes inside namespaces
If you define an operator << for a C++11 enum class, then you can use it successfully with Boost's unit test library.
However if you put the enum class inside a namespace, the Boost code no ...
3
votes
0
answers
58
views
"using" directive for declarator of scoped enum? [duplicate]
Is there a way to use some kind of using directive to directly access members of an enum class type?
enum class Foo {
Foo1,
Foo2,
...
};
int main() {
auto foo = Foo::Foo1;
??? // ...
0
votes
2
answers
261
views
How to use SFINAE to make a polyfill for missing values in enum class?
I thought this should be easy, but I've been struggling with it for a while so I thought I should ask here.
I want to make a template metafunction that takes a type corresponding to a C++11 enum ...
0
votes
2
answers
783
views
Inner scoped enumeration, hash function and unordered_set data member
I've the following problem of which I cannot find a solution.
Of course, it could be that a solution does not exist at all, but I'd like to have a try on SO before to give up.
First of all, a snippet ...
1
vote
0
answers
162
views
Getting errors because compiler confuses static enum class between C++11 and Managed C++
I have recently migrated a Managed C++ project from Visual Studio 2010 to 2013.
Inside the definition of a class, I have:
static enum class ItemType
{
SHOP=IDS_SHOP,
PAGE=IDS_PAGE,
PHOTO=...
3
votes
1
answer
384
views
Using enum class for defining flags
What is the appropriate pattern for using enumerations as flags in modern C++?
The question stems from my reading of the technical specification A Proposal to Add 2D Graphics Rendering and Display to ...
1
vote
1
answer
439
views
enum class ambiguous inheritance
I am really strugling with an inheritance issue when using what should be strongly typed enums - however the enums seems to be ambigious when used through inheritance.
Im using g++ (GCC) 4.7.2
enum ...
6
votes
1
answer
9k
views
Invalid use of incomplete type struct std::hash with unordered_map with std::pair of enum class as key
I want to use a unordered_map<std::pair<enum_class,other_enum_class>,std::uint8_t> for managing some pixelmap formats.
Here the minimal code :
#include <unordered_map>
#include &...
5
votes
1
answer
148
views
Type cast failed in switch for enum with restricted storage
SSCCE:
enum class confirm {yes};
struct item
{
confirm s:4; // (1) limiting storage size required
};
int main()
{
item itm;
itm.s = confirm::yes; // (2) OK
switch (itm.s)
{
case ...
1
vote
0
answers
480
views
Conditional enum as part of templated class
I am working on my c++ library for microcontrollers and I need a way to specify different enum class content based on the template type.
I have this code so far:
#include <cstdint>
#include &...
1
vote
1
answer
215
views
Enum class bitmasks used in template constexpr method
I have the following C++11 code in my microcontroller project:
template<std::uint32_t... I>
struct mask_or;
template<>
struct mask_or<> {
static constexpr std::uint32_t value = ...
16
votes
4
answers
7k
views
Wrap enum class with Cython
I am trying to wrap an enum class in a c++ header file for use in a cython project.
For example, how can this
enum class Color {red, green = 20, blue};
be wrapped with Cython.
7
votes
5
answers
3k
views
Use enum classes with Boost Test
I have an enum class that I would like to use in my unit tests:
enum class MyEnumClass
{
MyEntryA,
MyEntryB
};
I would like to use it as follows:
MyEnumClass myEnumValue = MyEnumClass::...
4
votes
1
answer
182
views
Modifying scoped enum by reference
I am increasingly finding scoped enums unwieldy to use. I am trying to write a set of function overloads including a template for scoped enums that sets/initializes a value by reference--something ...
1
vote
1
answer
358
views
c++ typedef/type substitution for enumeration class
As far as I am aware at the moment it is not possible to do a typedef of the C++11 enum class. I would like to know if there is any other way I can reduce the length of the name of a variable enum ...
0
votes
1
answer
138
views
define enum class with same name compared to enum [duplicate]
Why this compiles in c++11:
struct foo
{
enum class Resolution { None=10, Nominal=20 };
enum class Scale { None, Nominal };
};
while this doesn't:
struct foo
{
enum Resolution { None=10, ...
0
votes
1
answer
575
views
GoogleTest 1.7.0 `enum class` compile error
When I try and reference an enum class from a test fixture, it fails to compile with error ./gtest_mcp23s17.cpp:25:52: error: no type named 'HW_ADDR_6' in 'mcp23s17::HardwareAddress'
TC_mcp23s17 ...
2
votes
1
answer
921
views
Using `reinterpret_cast` on an enum class - valid or undefined behavior? [duplicate]
#include <iostream>
#include <cassert>
#include <type_traits>
template<typename T> using Underlying = std::underlying_type_t<T>;
enum class ETest : int
{
Zero = 0,
...
7
votes
3
answers
12k
views
How to Enable C++11 Features in Codelite
The following code compiles and runs in Xcode 5 and in Visual Studio 2013. I am interested in trying out Codelite, but Codelite will not compile the following program (a problem since I am working ...
1
vote
2
answers
163
views
Is there a simple way to convert an enum class to a string (c++)?
while there are solutions to easily convert an enum to a string, I would like the extra safety benefits of using enum class. Is there a simple way to convert an enum class to a string?
(The solution ...
8
votes
4
answers
10k
views
C++11 mixing enum class and unsigned int in switch case will not compile
Why doesn't this code compile, and what can I do to make it compile?
#include <iostream>
using namespace std;
enum class myEnum : unsigned int
{
bar = 3
};
int main() {
// your code ...
0
votes
2
answers
74
views
C++: How to get function to accept an object with same class name from any namespace?
MainClass.h:
namespace Alpha{ enum class Parameters; }
namespace Beta { enum class Parameters; }
MainClass{
public:
enum class Type{ Type_A, Type_B };
MainClass(const Type t);
void DoStuff(...
5
votes
1
answer
963
views
User-defined implicit conversion of an enum class when calling an overloaded operator fails
Consider the following example:
struct ConvertibleStruct {};
enum class ConvertibleEC {};
struct Target {
// Implicit conversion constructors
Target(ConvertibleStruct) {}
Target(...
6
votes
3
answers
12k
views
c++: enum inside of a class using "enum class"
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 ...
0
votes
0
answers
52
views
Why var.constant is not allowed for an enum class?
Suppose we have an enum class:
enum class E
{
constant
};
To refer to the enumerator in E, we can write E::constant, while the following is illegal:
E e;
e.constant;
But consider this:
struct S
...
1
vote
0
answers
259
views
Astyle C++ enum class indentation
Is there a way of getting Astyle to indent enum class elements to the same depth?
I am getting the following:
enum class test {
test1 = 1,
test2 = 2,
test3 = 3,
...
20
votes
3
answers
18k
views
Qt - Q_DECLARE_METATYPE() with an enum class type
Is there a way to use Q_DECLARE_METATYPE() with enum class types? I know old enums work but what about these new, strongly typed, ones? Can't find anything regarding this problem elsewhere. I'm using ...
31
votes
6
answers
10k
views
Why can't C++11 strongly-typed enum be cast to underlying type via pointer?
In C++11 we can cast a strongly-typed enum (enum class) to its underlying type. But it seems we cannot cast a pointer to the same:
enum class MyEnum : int {};
int main()
{
MyEnum me;
int iv = ...
9
votes
2
answers
2k
views
Is it possible to make a scoped enumeration ("enum class") contextually convertible to bool?
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<...
7
votes
2
answers
5k
views
enum class in QVariant in QSettings
I have a problem with enum classes, QVariants and the QSettings class. There are enum class values that I want to store within a QVariant which goes into a QSettings instance. So, my code actually ...
6
votes
2
answers
2k
views
Why enumeration cannot be a template?
enumeration cannot be a template is the error given when I try to compile with BCC64 (based on Clang) the following code:
template <typename T> enum class fooEnum : T
{
a,b,c,d,e
};
At ...
9
votes
1
answer
1k
views
Initialization of a static constexpr class member of enum-class type by explicit conversion function
I have a discrepancy between the behaviour of g++ 4.8.1 and clang++ 3.4.
I've got a class A, of literal type, that has an explicit constexpr conversion function to type enum class E.
Gcc allows me ...
5
votes
1
answer
9k
views
Implicit conversion from int to enum class in switch statement
enum class pid
{
Alpha, Beta, Gamma
};
int main()
{
int propId = 2;
switch(propId)
{
case pid::Alpha:
case pid::Beta:
case pid::Gamma:
break;
}
}
Above snippet ...