如果实际对象不是const,则具有写操作的const_cast(this)是否具有未定义的行为? [英] Is const_cast(this) with a write operation undefined behaviour, if the actual object is non-const?

查看:102
本文介绍了如果实际对象不是const,则具有写操作的const_cast(this)是否具有未定义的行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题的答案也启发了我的问题: https://stackoverflow.com/a/56989169/2492801 .

My question is inspired by this answer to another one of my questions: https://stackoverflow.com/a/56989169/2492801.

如果我有一个实际上不是const的对象,但是调用了它的const方法之一,则在方法this中当然是const.如果我const_cast离开其常数,并将其传递给对this指向的对象执行写操作的另一种方法,那是不确定的行为吗?

If I have an actually non-const object, but call one of its const methods, then inside the method this is const of course. If I const_cast away its constness and pass it to another method that performs a write operation on the object pointed to by this, is that undefined behaviour?

如果确实如此,我不会感到惊讶,因为this实际上是const方法中的const.另一方面,对象本身不是const,因此通常不禁止写操作.

I wouldn't be surprised if it was, because this is really const inside a const method. On the other hand, the object itself is non-const so write operations are not generally forbidden.

对我来说,重要的是要知道如何处理我的其他问题中所述的问题.谢谢!

For me it is important to know that to know how to deal with the problem described in my other question. Thank you!

推荐答案

那不是不确定的.这正是const_cast的用途.只要对象本身不是-const,则可以使用const_cast将该对象抛弃,并将其作为非const指针进行相同的操作.

That's not undefined. That's exactly what const_cast is for. As long as the object itself is non-const then you can cast it away with const_cast and do the same things with it as a non-const pointer.

请注意,const_cast通常被认为是代码异味,并且可能表明设计不好.

Do note that const_cast is usually considered a code smell and might indicate bad design.

如标准所说:

在非static([class.mfct])成员函数的主体中, 关键字this是一个prvalue,其值是指向该对象的指针 该函数被调用.成员函数中的此类型 类XX*.如果成员函数声明为const,则类型为 这是const X*,如果成员函数声明为volatile,则 它的类型是volatile X*,并且如果声明了成员函数 const volatile,其类型为const volatile X*.

In the body of a non-static ([class.mfct]) member function, the keyword this is a prvalue whose value is a pointer to the object for which the function is called. The type of this in a member function of a class X is X*. If the member function is declared const, the type of this is const X*, if the member function is declared volatile, the type of this is volatile X*, and if the member function is declared const volatile, the type of this is const volatile X*.

在您的情况下,this的类型为const X*,即使对象本身不是非const.

The type of this is const X* in your case even though the object itself is non-const.

标准const_cast:

对于两个类似的类型T1T2,类型T1的prvalue可能是 使用const_­cast显式转换为类型T2.这 const_­cast的结果引用原始实体.

For two similar types T1 and T2, a prvalue of type T1 may be explicitly converted to the type T2 using a const_­cast. The result of a const_­cast refers to the original entity.

因此,从const X*投射到X*也是合法的.

So, casting from const X* to X* is also legal.

最后,它说(尽管有注释):

Lastly, it says (albeit in a note):

[注:根据对象的类型,通过 由a产生的数据成员的指针,左值或指针 抛弃const限定符的const_cast可能会产生未定义的 行为([dcl.type.cv]). —注释]

[ Note: Depending on the type of the object, a write operation through the pointer, lvalue or pointer to data member resulting from a const_­cast that casts away a const-qualifier may produce undefined behavior ([dcl.type.cv]). — end note  ]

[dcl.type.cv] 告诉我们:

任何修改尝试([expr.ass],[expr.post.incr],[expr.pre.incr]) 一个const对象([basic.type.qualifier])在其生存期内 ([basic.life])导致行为不确定.

Any attempt to modify ([expr.ass], [expr.post.incr], [expr.pre.incr]) a const object ([basic.type.qualifier]) during its lifetime ([basic.life]) results in undefined behavior.

幸运的是,我们的this指向非const对象,因此强制转换并通过新的非const指针修改该对象不会触发未定义的行为.

Luckily, our this is pointing to a non-const object, so casting it and then modifying this object through the new non-const pointer doesn't trigger undefined behaviour.

抱歉.

这篇关于如果实际对象不是const,则具有写操作的const_cast(this)是否具有未定义的行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆