为什么我不能 static_cast char* 到 std::byte*?

NoS*_*tAl 1 c++ c++20 std-byte

我知道我可以使用reinterpret_cast,但是我不能从 char 转换为像 std::byte 这样的“通用”类型似乎很奇怪。这只是不幸的错误/限制,还是有原因?

示例

int main(){
    std::string s{"abc"};
    std::byte* ptr  = static_cast<std::byte*>(s.data());
}
Run Code Online (Sandbox Code Playgroud)

Yak*_*ont 5

静态转换仅适用于:

  1. 数字类型
  2. 可能相关的类类型指针/引用(向上和向下)。
  3. 指向/形成空指针的指针。
  4. 激活转换构造函数/运算符

这就对了。

将事物重新解释为字节是一种重新解释转换。