在一個變體類我工作的原始存儲是一個字符數組:的reinterpret_cast/static_cast並未定義的行爲
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));
相關問題/答案之間有什麼不同之處:(見James Kanze的評論)
https://stackoverflow.com/a/7321815/3235496。
編輯
第二個問題已經在這裏有一個答案:C++ When should we prefer to use a two chained static_cast over reinterpret_cast
當然這個'new(storage)X(x);'是內存泄漏 – 2015-02-07 12:51:21
@EdHeal它通過放置new來在存儲中構造X.對齊存儲應該是安全的做法(例如http://stackoverflow.com/questions/4583125/char-array-as-storage-for-placement-new)。你能添加一些細節嗎? – manlio 2015-02-07 13:00:39