【问题标题】:reinterpret_cast / static_cast and undefined behaviorreinterpret_cast / static_cast 和未定义的行为
【发布时间】:2025-12-13 06:10:02
【问题描述】:

在一个变体类中,我正在处理原始存储是一个 char 数组:

alignas(/* the strictest alignment of all types of the variant */)
char storage[/* ... */];

赋值运算符类似于:

template<class X>
void operator=(const X &x)
{
  // ...code for clearing the storage and setting the tag for type X...

  new(storage) X(x);
}

而获取存储对象的代码是:

template<class X>
const X &get()
{
  // ...
  return *reinterpret_cast<X *>(storage);
  // ...
}

它似乎有效,但它总是定义良好吗?我担心安全地取消引用指针(类型别名规则是否允许?)。

当前的实现和

有什么不同吗?
 return *static_cast<const X *>(static_cast<const void *>(storage));

相关问题/答案:

https://*.com/a/7321815/3235496(参见James Kanze 的 cmets)。


编辑

第二个问题在这里已经有了答案:C++ When should we prefer to use a two chained static_cast over reinterpret_cast

【问题讨论】:

  • new(storage) X(x);肯定是内存泄漏
  • @EdHeal 它通过placement new的方式在存储中构造X。对齐存储应该是一种安全的做法(例如*.com/questions/4583125/…)。你能补充一些细节吗?

标签: c++ c++11 variant reinterpret-cast static-cast


【解决方案1】:

由于storage 正确对齐,我无法想象哪里会出现问题。关于指针转换的段落(*) 4.10 说:“指向 cv T 的指针”类型的纯右值,其中 T 是对象类型,可以转换为“指向 cv void 的指针”类型的纯右值。将指向对象类型的指针的非空指针值转换为“指向 cv void 的指针”的结果表示与原始指针值在内存中相同字节的地址

关于您的第二个问题,第 5.2.10 段 在reinterpres_cast 上:对象指针可以显式转换为不同类型的对象指针。当对象指针类型的纯右值 v 转换为对象指针类型“指向 cv T 的指针”时,结果为static_cast&lt;cv T*&gt;(static_cast&lt;cv void*&gt;(v)),其中cv 代表可选的constvolatile。 p>

所以这部分是按照规格保证的。更多的是,我们看到void * 的强制转换应该指向内存的第一个字节,我对标准的理解没有UB ...只要编译器有相同的理解;-)

(*) 参考:Draft for current C++ specification

【讨论】: