有些事情我不明白reinterpret_cast。
cppreference中的以下片段讲述了整数值和指针之间的转换。
- 任何整型或枚举类型的值都可以转换为指针类型。转换为足够大小的整数并返回到相同指针类型的指针保证具有其原始值,否则无法安全地取消引用结果指针(不保证相反方向的往返转换;相同的指针可能会有多个整数表示)空指针常量 NULL 或整数零不保证产生目标类型的空指针值;为此目的应使用 static_cast 或隐式转换。
特别是,根据我对“相反方向的往返转换”部分的理解,C++ 不保证像这样的强制转换
uintptr_t x;
reinterpret_cast<uintptr_t>(reinterpret_cast<void *>(x)) // == x ?
Run Code Online (Sandbox Code Playgroud)
如果有这样的保证,最好看看相应的参考资料。
我的第二个问题涉及以下内容的正确性:假设int x; int y;和x == y为真,并且sizeof(int)严格小于sizeof(void *)。那么就保证是reinterpret_cast<void *>(x) == reinterpret_cast<void *>(y)真的吗?
直观上,假设物理内存中x后面有一些垃圾字节,并且y后面也有一些垃圾字节。然后通过测试reinterpret_cast<void *>(x) == reinterpret_cast<void *>(y)我们进行如下数值比较:
x garbage_x
==
y garbage_y
Run Code Online (Sandbox Code Playgroud)
为了澄清,x 指的是表达式中值为 x 的内存部分reinterpret_cast<void *>(x),并garbage_x指内存的剩余部分。
作为另一个特殊情况,这个与文字的行为是什么reinterpret_cast<void *>(1) == reinterpret_cast<void *>(1)?
我已按照标准程序(npm uninstall -g create-react-app和
yarn global remove create-react-app)删除了之前使用纱线全局安装的 create-react-app ,如下面的屏幕截图所示。我还检查了以下目录不存在:/usr/local/bin/create-react-app。
但是,在使用时我仍然遇到错误npx create-react-app myapp,说我正在运行 create-react-app 4.0.0。
which create-react-app
Run Code Online (Sandbox Code Playgroud)
也什么也不输出。