I am dealing with some code that uses an external library in which you can pass values for callbacks through a value void*.
Unfortunately, the previous person working on this code decided to just pass integers to these callbacks, dropping the integer to the void ( (void*)val) pointer .
Now I'm working on cleaning up this mess, and I'm trying to determine the "right" way to distinguish an integer from /. Unfortunately, fixing the use of void pointers is somewhat outside the scope of the refinement I can do here.
Right now, I'm doing two casts to convert from / to void pointer:
static_cast<int>(reinterpret_cast<intptr_t>(void_p))
and
reinterpret_cast<void *>(static_cast<intptr_t>(dat_val))
Since I am on a 64-bit machine, direct execution ( (int)void_p) results in an error:
error: cast from 'void*' to 'int' loses precision [-fpermissive]
The initial implementation worked with -fpermissive, but I am trying to get away from this for maintainability and error problems, so I am trying to do this “correctly”, for example. C ++ cast.
Casting is done directly in int ( static_cast<int>(void_p)) ( error: invalid static_cast from type 'void*' to type 'int'). My understanding reinterpret_castis that it basically just forces the compiler to treat the address of the value in question as a data type other than the type, without actually emitting any kind of machine code, so casting intdirectly to void*it would be a bad idea, because void*more than int(respectively 4/8 bytes).
, intptr_t , , void*, , , .
? , void?