ant*_*_rh 7 c++ undefined-behavior reinterpret-cast
下面的代码是UB吗?
int i = 5;
void *p = &i;
int* &r = reinterpret_cast<int* &>(p);
int* p2 = r;
Run Code Online (Sandbox Code Playgroud)
请注意,我不会取消引用指针。
是的,就是UB。
reinterpret_cast<int* &>(p);
Run Code Online (Sandbox Code Playgroud)
相当于
*reinterpret_cast<int**>(&p);
Run Code Online (Sandbox Code Playgroud)
reinterpret_castto是允许void**的int**,但隐式取消引用是 UB,因为数据类型 ( void*) 和正在访问的数据类型 ( int*) 不相似。