I read for a long time about a strict alias . C / C ++ standards say that the following code is invalid (undefined behavior must be correct), because the compiler may have aa cache value somewhere and will not recognize that it needs to update the value when updating b;
float *a;
...
int *b = reinterpret_cast<int*>(a);
*b = 1;
The standard also says that something char*could be an alias, so (correct me if I'm wrong) the compiler will reload all cached values whenever a variable is accessed char*. Thus, the following code will be correct:
float *a;
...
char *b = reinterpret_cast<char*>(a);
*b = 1;
But what about cases where pointers are not involved at all? For example, I have the following code, and GCC issues warnings about a strict alias on me.
float a = 2.4;
int32_t b = reinterpret_cast<int&>(a);
What I want to do is just copy the original value a, so strict overlay should not be applied. Is there a problem here, or is GCC just too careful about this?
EDIT
I know that there is a solution using memcpy , but this leads to the fact that the code is much less readable, so I would not want to use this solution.
EDIT2
int32_t b = *reinterpret_cast<int*>(&a); also does not work.
solvable
There seems to be a bug in GCC .
user283145
source
share