问题描述
const_cast
可用于删除 volatile
限定符:
The const_cast
can be used to remove volatile
qualifier:
特别是,只有const_cast可用于抛弃(删除)constness或volatility
In particular, only const_cast may be used to cast away (remove) constness or volatility
[]
但 volatile
对象应该是 NOT 以这种方式访问:
const_cast conversion - cppreference.com[^]
But the volatile
object should NOT be accessed that way:
任何尝试引用volatile对象通过非易失性glvalue(例如通过引用或指向非易失性类型的指针)导致未定义的行为
Any attempt to refer to a volatile object through a non-volatile glvalue (e.g. through a reference or pointer to non-volatile type) results in undefined behavior
[]
那么,重点是什么?
此外,在我看来,从实际的 const
对象中删除 const
cv(存储在标记为const的内存页面中)导致分段错误。
我尝试过:
我确认,当我试图通过 none volatile ref(使用<$ c)访问 volatile
对象时在 MS VS 2013 中的$ c> const_cast )它运行良好,通过编译在 MS VS 2015 它只是粉碎了,所以是的,这显然是UB 。
cv (const and volatile) type qualifiers - cppreference.com[^]
So, what the point?
Also, as i see it, removing the const
cv from an actual const
object (stored in the memory page marked as const) leads to the segmentation fault.
What I have tried:
I confirm that, when i was trying to access the volatile
object by none volatile ref (using the const_cast
) in MS VS 2013 it works fine, through compiled in MS VS 2015 it just crushed, so yep this is clearly UB.
推荐答案
这篇关于删除volatile cv的原因是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!