191 questions
2
votes
3
answers
179
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 };
...
4
votes
1
answer
146
views
product enum class in c++
Is there a way to automatically (and at compile time) define a "product enum class" (i.e. an enum class that takes on values in the product space between two given enum classes)?
So if I ...
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
vote
1
answer
62
views
Accessing the Enum Class used to make a Templated Struct C++
template <typename FruitEnum>
struct FruitManager
{
FruitEnum group
};
enum class FirstFruitEnum
{
apple_fruit,
banana_fruit,
};
enum class SecondFruitEnum
{
banana_fruit,
...
-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 ...
-2
votes
2
answers
201
views
how to know an enum class is sorted
how to know an enum class is sorted, for example:
// is sorted:
enum class data
{
type1 = 2,
type2 = 3,
type3 = 4,
type4 = 5,
type5 = 6
};
// not sorted:
enum class data
{
...
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 ...
3
votes
1
answer
168
views
DRY: C++11 enum class vs C enum
I have class like this:
struct InsertResult{
enum class Status{
INSERTED ,
UPDATED_IN_PLACE ,
REPLACED ,
SKIP_INSERTED ,
...
1
vote
1
answer
103
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:...
-1
votes
1
answer
66
views
is it possible to convert std::string into enum class in std::cin?
I'am new in cpp. Let's say we've got a class as follows:
class Animal {
public:
enum class Group {mammal, bird, reptile};
private:
Group group;
}
Now I'd like to ...
0
votes
2
answers
196
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 { //...
0
votes
1
answer
60
views
Function to return the value of a Map by key in an enum
I have an enum class which maps language locale to a list.
How can I fix the function getReservationFrequencies inside the companion object
to return the value of the map based on the locale (key)?
...
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
2
answers
140
views
Alias template with a switch over template parameters
Consider a alias template declaration, depending on a single template parameter that can have a finite number of values, like a class enum.
I would like to a employ using to define a type alias for ...
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 ...
0
votes
1
answer
128
views
Can't manage to call is_unsigned on the underlying_type of an enum class template parameter
I'm attempting to limit the use of this function to enum classes that have unsigned underlying types (and have AddSubtract as an enumeration) but I cannot for the life of me figure out the correct ...
3
votes
2
answers
524
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
1
answer
51
views
Trying to create a generic cast to str for several enum classes
I'm trying to create a generic function toStr() for several enum classes but I'm having some issues.
I have these enum classes and maps to convert the value of the enums to string.
'''
enum class ...
0
votes
0
answers
28
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. ...
1
vote
1
answer
595
views
enum class nested in a class compiles on Linux, but not MacOS?
I wrote a basic program to show the issue I'm dealing with.
#include <iostream>
using namespace std;
class SomeClass {
private:
public:
enum class SomeEnum : int {a, b, c};
...
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 ...
0
votes
2
answers
924
views
How to iterate over enumerators of an enum class?
Is there a way to initialize a container (e.g. std::unordered_set<char>) with the enumerators of an enum class?
I have this class:
#include <iostream>
#include <unordered_set>
class ...
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&...
14
votes
3
answers
16k
views
What are commonly-used ways to iterate over an enum class in C++?
I am finding that all of my standard techniques for iterating over regular enums unfortunately do NOT work on enum classes since enum classes do not implicitly convert to integers.
NOT a duplicate of ...
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:...
0
votes
1
answer
130
views
Enum class to string
I’m having a solution with switch cases but there are many cases so clang-tidy is giving warning for that function. My motive is to decrease the size of function. Is there any way that we can do to ...
2
votes
4
answers
719
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 ...
0
votes
0
answers
149
views
Unable to Intialize Enum Class Types | Visual C++
Consider an enum class
enum class FOO
{
A,B,C
};
struct something
{
FOO abc = FOO::A; //Compiler Doesnt like this
}
int main(){
something _something;
return 0;
}
So the compiler doesnt like ...
3
votes
1
answer
871
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
};
2
votes
2
answers
1k
views
Safe and maintainable way to convert int to enum class
I am looking for a safe and maintainable way to convert an int value to enum class. I know i can convert inegral values to enum class by simply using static_cast, but i want to make sure the value to ...
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
1
answer
675
views
initializing enum with a constructor
I already found this really good explanation Initialising enum via constructors but it didn't fit my needs.
So I declare an enum inside a class and want to initialize it inside the class constructor ...
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
128
views
How to return an enum by item index as apposed to value index? [duplicate]
How do I index into an enum via "item index" instead of the "value indexing":
"Value indexing" (not useful to me with what I am doing):
eSerialBauds eBaud = static_cast&...
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 ...
1
vote
1
answer
1k
views
How to query validity of enum class used as bit mask without casting to underlying type
I have an enum class I use for bit masking, like so (in Unreal Engine, therefore the uint8 type)
enum class Level : uint8
{
None = 0x0,
Debug = 0x1,
Info = 0x2,
Warning = 0x4,
...
}...
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()
{
...
2
votes
1
answer
855
views
Is it possible to mark a enum class member as deprecated? [duplicate]
I've gotten in the habit of using the new enum classes (or strong enums) in C++. Now, I need to deprecate a member.
However, the standard enum deprecation syntax, as described in the following ...
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"?
2
votes
1
answer
914
views
Convert enum class to lvalue reference to its underlying type in C++
I have the following code in C++11 (drastically simplified to leave out the inessentials):
#include <cstdint>
#include <type_traits>
enum class State : std::uint8_t
{
Shutdown,
...
0
votes
0
answers
188
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 ...
8
votes
2
answers
5k
views
Can you use bitwise operators with enum classes without casting?
I like to use enum classes, but I use them as flags sometimes and I have to constantly cast to int if I want to use a bitwise operator. Is there a way to do this without casting? I don't think you can ...
0
votes
1
answer
74
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 ...
0
votes
0
answers
98
views
Operator[] for map failing to compile with use of scoped enum as key, but it's at() function works. Why the ambiguity?
I have code that looks like this:
enum class MyTypes {
TYPE_1 = 1,
TYPE_2 = 2,
TYPE_3 = 3
};
static const std::regex reg1("some unique expression");
static const std::regex reg2(&...
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, ...
4
votes
1
answer
9k
views
What's the difference between typedef enum and enum class?
I'm probably missing something but I'm working with a code base that uses a lot of
typedef enum foo
{
....
} foo;
Is this just the same as an enum class but not strongly typed?
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 ...
2
votes
1
answer
644
views
Why static_cast enum class that underlying type is int8_t to get an unexpected value? [duplicate]
compiler : clang++
c++ standard : c++20
I tried to run the code, and the results met my expectations very well.
#include <iostream>
using namespace std;
int main()
{
enum class Num : ...
8
votes
1
answer
809
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 ...
1
vote
1
answer
816
views
C-style cast of enum class to reference of underlying type char
Is this legal C++ (>=14), resulting in a char being read and saved into aCode?
enum class ECode : char { Code1 = 'a' };
std::istream& operator>>(std::istream& aIn, ECode& aCode)
{
...