191 questions
2
votes
3
answers
175
views
Can enum values be redeclared?
The C++ Coding guidelines, Enum.3: Prefer class enums over “plain” enums contains following example:
void Print_color(int color);
enum Web_color { red = 0xFF0000, green = 0x00FF00, blue = 0x0000FF };
...
29
votes
1
answer
2k
views
What is the purpose of `enum class` with a specified underlying type, but no enumerators?
The std::align_val_t type is defined as:
namespace std
{
enum class align_val_t : size_t
{
};
}
What is the purpose of such an empty enumeration?
What's the difference with a typedef?
-1
votes
2
answers
71
views
C++ idiom for a map of template argument to value of template-instance type?
I have an enum class, say
enum class color { Red, Green, Blue, /* etc. */};
and I have class template,
template <color Color> class foo;
Now, I want to declare a data type which holds, for ...
0
votes
0
answers
86
views
I want to know how to use when without else in an enum class in kotlin
I learned to use enum class to constantize the constants involved.
I also learned that using when when using enum class is very effective.
I think I heard that you may not use else.
Case1 may not use ...
1
vote
1
answer
100
views
operator<< for enum class just doesn't work
I have the following code:
logging.hxx
#include <iostream>
#include <string>
#include <sstream>
#include "singleton.hxx"
#include "utils.hxx"
class FGaugeLogger:...
0
votes
2
answers
194
views
How can I implicitly convert an enum to its subset and vice versa in C++?
More precisely, the feature I want is like implicitly convert an enum to its subset enum and vice versa.
The code I wish it working:
enum class Human {
A = 1,
B = 2,
};
enum class Male { //...
5
votes
2
answers
304
views
Dispatch on execution policy by type or enum?
In C++ I basically have two choices in policy based design patterns: I can use individual types (based on which an overload is selected) or specify an enum that contains all policies and I would ...
1
vote
1
answer
152
views
Any way to get a warning for static_cast<some_enum_class>(T) where T's type isn't the underlying type of some_enum_class?
I'm reviewing a lot of code where I need to ensure there are no static_cast (or any cast) calls on variables that could be out of range of the enum class that is being cast to. Ideally I'd be able to ...
3
votes
2
answers
519
views
Is using enum class for flags undefined behavior?
I've been using overloaded operators as demonstrated in the second answer from here: How to use C++11 enum class for flags ... example:
#define ENUMFLAGOPS(EnumName)\
[[nodiscard]] __forceinline ...
0
votes
0
answers
27
views
odd behavior with enum struct and compound assignment operator [duplicate]
I observed some behavior relating to enum structs and compound operators that I don't understand (yet). When using an enum struct as a flag, I'd like to write stuff like flag &= enum_name::What. ...
2
votes
3
answers
1k
views
Using a enum class from a c++ header in a c header
I am writing a c wrapper around a c++ library.
In the c++ there are enum classes used as types for function arguments.
How do I use theme correctly in the c header.
One ugly way would be to use int's ...
5
votes
3
answers
1k
views
Avoiding repetitive copy-paste of static_cast<size_t>(enum_type) for casting an enum class to its underlying type
I have an enum type in my code, like this:
enum class GameConsts: size_t { NUM_GHOSTS = 4 };
I find myself repeating the required static_cast to get the enum value:
Ghost ghosts[static_cast<size_t&...
0
votes
1
answer
22
views
Is there an accepted way to adjust this so that all of my `enum class` elements are grouped together in the ClassView?
Old code:
typedef enum tagUndoAction { UNDO_CHANGE_CELL = 0,
UNDO_CHANGE_SELECTION_START,
UNDO_CHANGE_SELECTION_SUB } UNDO_ACTION_E;
New code:...
2
votes
4
answers
717
views
I want to make a C++ enum of QString formats to display a QTime
I've been working with C++ on a Time class in Qt and I need to write an enum class that contains the formats that I use in showTime(). Because of the nature of the QString formats I've been getting ...
3
votes
1
answer
870
views
why does define conflict with enum class?
Here is a small code in which the conflict occurs. is there any way to get around this correctly?
#define DEBUG 0
enum class TypeEnum : int
{
DEBUG = 0,
INFO = 1
};
7
votes
3
answers
1k
views
Check if enum class contains a specific identfier
I searched a bit here on SO and was surprise that I didn't find any similar question. Happy for any hints in case this has already been answered.
I have a codebase with a lot of enum classes defined. ...
0
votes
0
answers
164
views
Alternative for casting strongly-typed enum to int in C++
To convert a strongly-typed enum to int we can use:
enum class MyEnum { a, b };
int x = static_cast<int>(MyEnum::a);
what if i use the following line, which is shorter:
int x = int(MyEnum::a);
...
0
votes
2
answers
1k
views
Implicit conversion operator function from bool to enum class
I have a function to change the state of an LED that takes in an enum argument with three possible values:
enum class Command { Off, On, Toggle };
void led(Command);
I'd like to be able to use the ...
0
votes
2
answers
2k
views
why we cannot print the value of enum class as enum in c++
Getting error while trying to print the enum class object. I am getting the error while trying to print this. where am I doing mistake?
#include <iostream>
using namespace std;
int main()
{
...
3
votes
2
answers
7k
views
Convert Enum value to integer in "Enum" and "Enum Class"
What is the difference between the Enum and Enum Class and how to converting Enum value to the integer in "Enum" and "Enum Class"?
0
votes
0
answers
184
views
Access an array/map with index of enum class type (something like a constexpr map)
Say I have a
enum class foo : std::uint32_t {
a = 4711,
b = 815,
...
};
with n enumeration constants. Assume that there actual numerical value is important. I need to pass precisely one ...
0
votes
1
answer
72
views
Expose a private std::bitset field that is inside a class for modification
I'm coding in a C++ project that hasn't advanced beyond C++11 yet.
Let's say I have an enum class as follows:
enum class Weekdays
{
kSunday = 0,
kMonday,
...
kSaturday,
};
I want to ...
4
votes
3
answers
1k
views
In a template function, How do I use std::underlying_type just if type of the input is enum class?
I have a piece of code that returns the value of some bits of a given number (I counted enum classes as a number too, using a static_cast).
template<typename Type>
bool get_bits(Type input, ...
3
votes
4
answers
11k
views
why can enum class values of type int not be used as int
I wanted to change an old-style enum to enum class : int because of its own scope.
But the compiler complains about using the values in integer arithmetics.
But why - since the enum is explicitly ...
8
votes
1
answer
808
views
Statically distinguish between an enum and an enum class in C++?
I have an event handler class that uses a template argument to set the event type. I want to enforce these event types to be enum classes of one byte size. Static assertion against the size is not an ...