[英]Is const_cast on this pointer undefined behavior?
In another question I ran across this code: 在另一个问题中,我遇到了这个代码:
Real StatData::mean(Real trim) const
{
// trim, pun not intended
const_cast<StatData&>(*this).items.sort();
// trim
}
cppreference also has an example on their page : cppreference在他们的页面上也有一个例子:
struct type {
type() :i(3) {}
void m1(int v) const {
// this->i = v; // compile error: this is a pointer to const
const_cast<type*>(this)->i = v; // OK
}
int i;
};
Aside from the obvious question of why this would ever be practical, is it unsafe? 除了明显的问题,为什么这将是实用的,它是不安全的? Does it matter of the object created is const or not and more importantly, is the
this
pointer safe from undefined behavior because it's only marked const
for that one function? 是否为const创建的对象是否重要,更重要的是,
this
指针是否可以保护未定义的行为,因为它只标记了该函数的const
?
Does it matter of the object created is const or not
创建的对象是否与const有关
Yes. 是。 If the object was created non-
const
, then no matter what you do the const_cast
will be safe. 如果对象是非
const
创建的,那么无论你做什么, const_cast
都是安全的。 Just a bad idea (because in general you don't know what the object was created non- const
). 只是一个坏主意(因为通常你不知道对象是非
const
创建的)。
the this pointer safe from undefined behavior because it's only marked const for that one function?
这个指针可以防止未定义的行为,因为它只标记了那个函数的const?
That's the wrong question really, but the point is that until you try to modify a const
object you're safe. 这确实是错误的问题,但问题是,在您尝试修改
const
对象之前,您是安全的。 That means the const_cast
itself is perfectly legal and well-defined, but if items.sort()
modifies stuff (and we have to assume it does) then that operation results in UB. 这意味着
const_cast
本身是完全合法且定义良好的,但如果items.sort()
修改了东西(我们必须假设它),那么该操作会导致UB。
Finally, although you've tried to gloss over this point in your question, the practicality of this is actually fundamental to the scenario: it's remarkably difficult to guarantee, even in this seemingly specific case, that the code is safe. 最后,虽然你已经尝试在你的问题中掩盖这一点,但实际上这是实际情况的基础:即使在这个看似特殊的情况下,代码是安全的,也很难保证。 So, don't do it .
所以, 不要这样做 。 Despite your attempts at abstracting it away for the purposes of this question, I can't stress that enough.
尽管你出于这个问题的目的试图将其抽象出来,但我不能强调这一点。
const_cast
itself is never undefined behavior. const_cast
本身永远不是未定义的行为。 It could be ill-formed, ie fail to compile. 它可能是不正确的,即无法编译。 But if it is well-formed (ie compilable) then it cannot produce undefined behavior by itself.
但是如果它是格式良好的(即可编译的)那么它本身就不能产生不确定的行为。
Undefined behavior might be triggered by what you do later with the [non-constant] access path you obtained with the help of const_cast
. 以后使用
const_cast
帮助获得的[非常量]访问路径可能会触发未定义的行为。 But it is a completely different story, not directly related to const_cast
. 但这是一个完全不同的故事,与
const_cast
没有直接关系。
The code samples you posted so far are insufficient to tell whether they exhibit undefined behavior or not. 您到目前为止发布的代码示例不足以判断它们是否表现出未定义的行为。 It all depends on the external factors: on whether the object being modified is really declared as
const
or not. 这一切都取决于外部因素:被修改的对象是否真的被声明为
const
。 An attempt to modify const
object triggers undefined behavior. 尝试修改
const
对象会触发未定义的行为。
For example, for the type
class above, this code triggers UB 例如,对于上面的
type
类,此代码触发UB
const type t;
t.m1(42); // <- undefined behavior
while this one does not 而这一个没有
type t;
t.m1(42);
assert(t.i == 42);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.