How to convert between int * and enum *?

From some C code, I get several constants like int *. In the C ++ part, I have an enumeration of the int base type. Converting between enum and int based on a single value works. However, a conversion between int *and is enum *not possible. See the sample code below.

Why is this and how to convert a pointer to some int values ​​to a pointer to int enums and vice versa? I kind of expect it to work since operations with the same value work, and the base types are the same. I read about What happens if you do not statically indicate the value of the enum class? but could not determine if invalid values ​​are playing here.

int i = 3;
enum E : int;
E e;

e = static_cast<E>(i);   // ok
i = static_cast<int>(e); // ok

int *j;
E * f;

j = static_cast<int *>(&i); // ok
f = static_cast<E *>(&i);   // 'static_cast': cannot convert from 'int *' to 'E *'
j = static_cast<int *>(&e); // 'static_cast': cannot convert from 'E *' to 'int *'

// now use j and f
*j = *f;
+4
2

?

int* E* , static_cast .

int int enums ?

reinterpret_cast static_cast:

f = reinterpret_cast<E *>(&i);
j = reinterpret_cast<int *>(&e);

reinterpret_cast:

T1 cv T2

, f j (.. *f *j) ( . ). , , , .

+3

" " int OP. , E* e, E int, int. .

++ , enum () , short, , /

, E* int*pi , pi . int* to E* , .

, , , :

. , .

: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3690.pdf (. 95 . 156)

, ( ), : E* - const int*, E* int, () int * .

, .

0

Source: https://habr.com/ru/post/1683680/


All Articles