reinterpret_cast / static_cast 和未定义的行为

reinterpret_cast / static_cast and undefined behavior

在一个变体中class我正在处理原始存储是一个字符数组:

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));

相关question/answer:

(see James Kanze 的评论)。


编辑

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

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

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

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

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