191 questions
1
vote
1
answer
1k
views
Document enum class values using Doxygen without enabling EXTRACT_ALL
I wasn't able to show documentation for values of enum classes without setting EXTRACT_ALL. The comments for preserve, truncate and append aren't there. The enum itself is documented. If I enable ...
1
vote
2
answers
594
views
C++11 How to cast enum class template parameter to int?
I have a struct which takes enum class value on the template parameter.
template <typename EnumValue>
struct Type
{
public:
static constexpr int VALUE = static_cast<int>(EnumValue);
};
...
1
vote
5
answers
4k
views
How do I switch over an enum class?
Enum classes are supposed to be strong enums in the sense that they don't implicitly convert to and from int. For instance:
enum class EC { a, b };
However, when switching over such a "strong enum":
...
7
votes
3
answers
610
views
Neat way how to cyclically iterate 4 enum class values in both directions in C++?
I have:
enum class orientation {
North,
East,
South,
West
};
I want to rotate its instance left (North => West) and right (West => North).
But I don't want to convert them to numbers, because ...
0
votes
1
answer
166
views
Forward declared enum as class member variable
As a rule of thumb on forward declaration (from "API Design for C++", p. 214), I only include the header of a class if I:
use an object of that class as a data member in my own class, or
inherit from ...
1
vote
2
answers
1k
views
I have a enum class in a .h and operator overloading in a .cpp error: no match for ‘operator<<’
OperatingSystem.h
#ifndef OPERATING_SYSTEM_H
#define OPERATING_SYSTEM_H
#include <iostream>
enum class OperatingSystem
{
unknown,
android,
iOS,
macOS,
Linux,
...
1
vote
1
answer
163
views
How to forward declare enum class as inner class of a templated class?
I'm abstracting the interrupt vector table on multiple microcontrollers.
I am using template classes in the form of
(InterruptVectorTable.hpp(definition, included in implementation))
template<...
2
votes
2
answers
2k
views
How should we use an enum class for indexing (or should we better avoid this)?
Say we have an enum type foo which we want to use to index an array arr of static size.
If we want to use an enum class for this, we could try it like this:
enum class foo
{
a,
b,
c,
...
4
votes
2
answers
1k
views
"Other"/Default-name behavior on a python Enum
I'm trying to achieve the following behavior in a python "enum" (so far to no success):
Given the enum class
class MyEnum(enum.Enum):
A=1
B=2
C=3
I want to have an "Other" member such ...
1
vote
0
answers
2k
views
Testing against values of an enum class in google test
I have a constructor of an object that can throw an exception if a parameter of an enum class type is wrong. This permits me to ensure that such an object, that will use that parameter for some ...
5
votes
3
answers
2k
views
Handling of switch enum class returns in clang, gcc and icc consistently
I am generally using clang to develop code, using all reasonable warnings I can (-Wall -Wextra [-Wpedantic]). One of the nice things about this setup is that the compiler checks for the consistency of ...
0
votes
2
answers
389
views
How to include enum class as class template arguments?
I'm trying to write a generic container class that I can use as a library. I want to use it to instantiate each object in a collection exactly once. So I thought to pass in both the pointer to the new ...
5
votes
1
answer
7k
views
Use 'using enum' in C++20 in classes possible?
In this answer it was mentioned that in the upcoming C++20 standard it is possible to use the using statement on enum class and import the enum fields into the local namespace.
I was wondering if ...
2
votes
1
answer
125
views
meaning of code snippet involving enum class and macro expansion
I encounter the following code in C++. I am new to the language and am not familiar with the syntax.
I understand the basic of macro expansion and enum class.
I learn that the macro etokenize with ...
5
votes
0
answers
532
views
How to check if a type is an enum class? [duplicate]
With the following code I can check at compile time if type E is an enum:
static_assert(true == std::is_enum<E>::value, "Must be an enum");
How can I check if it is an enum class?
Here they ...
2
votes
1
answer
2k
views
Operator overloading of enum class in class
I'm using private enum class in nested class and want to implement operator! for my enum class.
I know how to do this. But when I tried to overlord operator of enum class in nested class, compiler ...
0
votes
1
answer
2k
views
Enum Class Operator Overload no match found for operator
I'm attempting to overload some operators for an Enum class.
I get a compiler error saying it's unable to find the operator
In Enum.h
enum class SomeEnum : unsigned
{
Test0 = 0,
Test1 = (1 &...
0
votes
1
answer
1k
views
Brace initialised static const unordered_map using enum class
I'm having issues using an enum class as a type specifier within an unordered_map.
I've trawled the internet but had no luck with any of the solutions.
Closest example I found was at the link below, ...
0
votes
1
answer
632
views
enums and enum classes in Qt Queued Slots
Following advice from Qt documentation and this question's answers, I have code structured like so:
emulator.h:
class Emulator : public QObject
{
Q_OBJECT
public:
enum HaltCause ...
0
votes
1
answer
172
views
Template optimization for enum base type
I have the two code examples below. Both of them interprete an enum or enum class as their underlying type. Which one would be smaller after compilation when using multiple different enums?
Working ...
2
votes
1
answer
1k
views
Is there any way to get random from enum class in c++?
I want to fill a variable with random element from enum class.
So I tried set enum class type to int and pass last enum from enum class to rand:
enum class Enumerator: int
{
en1=0,
en2,
...
4
votes
2
answers
990
views
Scoped Enums (enum class) relational operators
I've looked all over the place and I can't believe this question has not been asked before.
Is the ordering of scoped enumerators defined by the standard?
Say if I have the following
#include <...
0
votes
0
answers
33
views
Placing definition of enum class, used as template parameter for a template class, inside the template class
I like to define my enum's that belong to a certain class inside that class. That makes it clear they belong together and helps against namespace pollution:
class Bar
{
public:
enum class Foo
...
9
votes
2
answers
6k
views
C++ enum class: Cast to non existing entry
I have this situation on one Project where we have some socket-communication that mainly exchanges characters for flow-control.
We cast those characters to an enum class : char in a switch.
I was ...
0
votes
0
answers
69
views
Given a "black box" enum, is it possible to check if a provided integral is a valid member of the enum in C++?
I'm writing an EnumSet class for a project. I would like to have an EnumSet::AllOf() method if possible, like in Java. The obvious way to go about this is to iterate over every member of the std::...
3
votes
1
answer
766
views
What is the C++ equivalent of Java's EnumSet.allOf()?
I'm writing a simple Naive Bayes image classifier in C++. I'd like to parametrize it by two enum class types (one for the type of input pixels, one for the class of the image). The problem is that ...
0
votes
2
answers
663
views
proper placement of a hash table for enum class
I am working on a senior project and having a question about how to best implement a lookup table for my program. There are several enum class files that contain enum classes and an operator<< ...
2
votes
1
answer
606
views
fail proof conversion of string to enum class
I have an enum class like this (I am planning to add more options to it later):
enum class ViSequencePointType {
JumpToValue = 0,
RampToValue = 1
};
Then I have a configuration text file which ...
2
votes
2
answers
2k
views
(De)serializing an enum class
I am trying to serialize and deserialize (using QDataStream but that is irrelevant here) an enum class variable:
enum class Type : char
{
Trivial,
Complex
};
The serialization is easy:
...
0
votes
2
answers
104
views
How to reuse operator overloads for different types?
I have several enums defined as follows:
enum class Suit {
spades = 1, hearts, diamonds, clubs,
first = spades, last = clubs
};
enum class Rank {
six = 6, seven, eight, nine, ten, jack, ...
0
votes
1
answer
186
views
C++11 enum class namespace block
I have an enum class in C++11:
enum class eDays{ SUNDAY, MONDAY, /*...*/ };
The enum class sets the namespace for the values so it has to be used like:
eDays::SUNDAY
I want to set an namespace block ...
11
votes
2
answers
384
views
Adding bitwise operations and conversion-to-bool to scoped enums - a Christmastide exploration
Let's say that I'm crazy and decided to create the following monstrosity:
#include <type_traits>
#include <iostream>
// Utility proxy type - convertible back to E but also permits bool ...
0
votes
1
answer
575
views
c++ Using a a template function with enum class and overloaded conversion operators
I was reading the sample code on another post Specializations only for C++ template function with enum non-type template parameter
and I'm trying to take it one step further, by using a overloaded ...
0
votes
1
answer
255
views
Using enum class type in switch causing errors
I was hoping someone could help me out with an error I'm having using an enum class in a switch case. trying to use the traverse type to choose the path in the switch.
Here is my code:
enum class ...
-2
votes
1
answer
3k
views
How to initialize `std::set` in constructor parameter?
I have a construction that takes a std::set as a parameter.
How do I initialize the set in the constructor parameter?
Here's an minimal conceptual example. Actual implementation is much larger.
#...
14
votes
2
answers
786
views
Enum bitfield and aggregate initialization
The following code is accepted by clang 6.0.0 but rejected by gcc 8.2
enum class E {
Good, Bad,
};
struct S {
E e : 2;
int dummy;
};
S f() {
return {E::Good, 100};
}
Live godbolt example
...
3
votes
2
answers
6k
views
How do I check equality of two enum class elements with googletest?
I have an object that has an enum type as a member.
enum class Color { Blue=1, Green=2, Red=3};
struct A {
int f;
Color color;
A(int x, Color c) : f(x), color(c) {}
};
struct B{
......
8
votes
1
answer
311
views
Class vs enum class as an index type
P0138R2 proposal begins with1
There is an incredibly useful technique for introducing a new integer type that is almost an exact copy, yet distinct type in modern C++11 programs: an
enum class ...
0
votes
2
answers
2k
views
How to access enum class through index?
enum class Fruit { apple, orange, pear };
enum class Color { red, green, orange };
template <typename T> struct Traits;
//I have to return the appropriate value(string) of color and fruit in ...
2
votes
1
answer
302
views
Simple macros to check if enum classes are available
There are a lot of feature test macros in C++ which give a simple and portable way to detect the presence of C++ standards and experimental features. However, I didn't manage to find simple macros to ...
1
vote
3
answers
3k
views
How can I write an enum class to a file and access it in C++?
I am trying to write the contents of a class object into a file. The object has an enum class member and I am unable to write it into a file using ofstream.
I get the following error.
error: no ...
-2
votes
1
answer
325
views
why doesn't "cout << Color::green" compile?
I had this question on a test.
I know that I can do something like:
enum class Color { red, green = 1, blue };
Color c = Color::blue;
if( c == Color::blue )
cout << "blue\n";
But when I ...
0
votes
1
answer
80
views
How to check values inside enum class?
I wanted to check if the tags returned from a json object has one of the values inside an enum class.
Gson gson = new Gson();
AnalysisResult result = gson.fromJson(data, AnalysisResult....
3
votes
2
answers
1k
views
What is happening when calling an enum/enum class with parentheses in C++?
This is maybe a bit weird question, but I really don't know how to phrase it better than this.
I just discovered that I can do the following:
#include <iostream>
enum class Colour // also ...
3
votes
3
answers
1k
views
Define enum element value using an operation on another enum values
Here is what I need to do: define, inside a class, two enumerations, the second having elements defined using elements values from the first.
So something like this:
class MyClass
{
public:
...
8
votes
3
answers
8k
views
C++ enum class std::size_t implicit conversion
I have defined a tuple and its indices by creating an enum class:
/** parameter { key ; value1 ; value1 ; } */
using Parameter = std::tuple<unsigned, unsigned, unsigned>;
enum class ...
-1
votes
1
answer
480
views
'enum struct' won't compile unless <iostream> is included [duplicate]
I have code that contains the line
enum struct cols: int8_t {red, blue, green};
When i compile this, i get errors:
test.cpp:4:1: warning: elaborated-type-specifier for a scoped enum must not use ...
0
votes
1
answer
897
views
Why is the constructor undefined?
Even though I'm working with Bukkit, this is a Java problem;
I don't know, why Java says the constructor is undefined, since it is defined
import org.bukkit.entity.EntityType;
import net.minecraft....
1
vote
2
answers
1k
views
Keys to access vector fields - enum class or enum in namespace?
Let's say I have an alias for vector:
typedef std::vector<double> PlanetData;
And I want it's fields to be accessible via some keys:
double x = planet_data[PlanetDataKeys::PosX]; //planet_data ...
1
vote
2
answers
2k
views
The out of range value of enum class
When I define an enum class inside a function, it has a value from the available options. However, when I define it within a class, it has value of none of the options. So what is the initial value of ...