interpret_cast/static_cast和未定义的行为
reinterpret_cast / static_cast and undefined behavior
在一个变体类中,我正在处理的原始存储是一个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://stackoverflow.com/a/7321815/3235496(见詹姆斯·坎泽的评论)。
编辑
第二个问题在这里已经有了答案:C++我们什么时候应该更喜欢使用双链static_cast而不是repret_cast
由于storage
正确对齐,我无法想象哪里会出现问题。关于指针转换的第(*)4.10段说:类型为"pointer to cv T"的prvalue,其中T是对象类型,可以转换为类型为"指针to cv void"的prvalue。将指向对象类型的指针的非null指针值转换为"指向cv void的指针"的结果表示内存中与原始指针值相同字节的地址。
关于你的第二个问题,第5.2.10段在reinterpres_cast
:对象指针可以显式转换为不同类型的对象指针。当对象指针类型的prvalue v转换为对象指针类型"pointer to cv T"时,结果为static_cast<cv T*>(static_cast<cv void*>(v))
,其中cv
代表可选的const
或volatile
。
所以这部分是有保证的规格。正如我们所看到的,void *
的强制转换应该指向内存的第一个字节,对于我对标准的理解来说,没有UB。。。前提是编译器具有相同的理解;-)
(*)参考:当前C++规范的草案
相关文章:
- 编译C++时未定义的引用
- vscode g++链路故障:体系结构x86_64的未定义符号
- 如何修复此错误:未定义对"距离(浮点数,浮点数,浮点数,浮点数,浮点数)"的引用
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- 不知道某个东西是否被忽略会引入未定义的行为吗
- 对C宏的未定义引用,但在定义它时会出现重新定义错误
- 未定义的引用在哪里
- 编译时的 CImg 库返回对"__imp_SetDIBitsToDevice"的未定义引用
- 对Py_Initialize()的未定义引用
- c++11评估顺序(未定义的行为)
- 使用mysql c++连接器的未定义引用
- 从python调用openMP共享库时,未定义opnMP函数
- 在 Mac 上使用 CMAKE 将 FFTW 和 FFTWPP 链接到项目中时未定义的符号
- Cmake 链接问题:未定义对 Button::mousePressEvent(QGraphicsSceneMouseE
- 未定义的引用 .. 使用 OpenCV 编译 C++ 代码时,从命令行
- 具有外部"c"和程序集的未定义函数
- 此增量后语句是否会导致未定义的行为?
- 尝试调用 .h 文件中定义的变量时出现变量未定义错误
- 在C++中使用内联方法时出现未定义的符号错误
- 对 Scalar ::Scalar() 的未定义引用