I have an enum
which looks like this:
enum class MY_TYPE : int32_t{
UNKNOWN = 0,
A = 101,
B = 102,
C = 103,
D = 104,
E = 105
};
I want to cast given values from and to it.
First of all, is it guaranteed that the following code does not induce undefined behavior?
int32_t library_func(int* tp){ *tp = 275; return 0;}
int main(){
MY_TYPE d = static_cast<MY_TYPE>(224);
library_func(reinterpret_cast<int*>(&d));
std::cout << "d is " << static_cast<int>(d);
}
Is it UB if I cast a value not inside the value range of MY_TYPE
? About this, I found the following issue: What happens if you static_cast invalid value to enum class?
This prints 275
as expected. Do I understand the answer correctly that casting any value which fits in the enum
's underlying type is fine?