I had some old code that worked like a charm, "suddenly" strange errors appeared. I can reproduce the problem with the code bellow:
int main(int argc, char **argv)
{ void* p=0;
int b=(int)p; // line1: this produces compile error at least for gcc std=c++23
int a=(int)(long)p; // line2: this line emits a warning only
return 0;
}
The error is: "error: cast from 'void*' to 'int' loses precision [-fpermissive]"
As I have to "downcast" the pointer p to an integer ( in reality in my case the correct downcasting should be from size_t to uint or from ptrdiff_t to int ).
I don't want to switch off warnings, so I found the "trick" to convert at first to a long (or more correctly should be a ptrdiff_t and afterwards convert it again to an int). In such a way I bypass "frontier controls".
BUT SEMANTICALLY both cases are interpreted by me as as being the same and should compile equivalently.
So I am asking what should be done to implement the code in the more standard and clean way ?
Isn't there a gap in logic ? For the story i compile using g++ and -m64.
The question is mostly theoretical by the way.