如果实际对象不是const,则具有写操作的const_cast(this)是否具有未定义的行为? [英] Is const_cast(this) with a write operation undefined behaviour, if the actual object is non-const?
问题描述
这个问题的答案也启发了我的问题: 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,其值是指向该对象的指针 该函数被调用.成员函数中的此类型 类X
是X*
.如果成员函数声明为const,则类型为 这是constX*
,如果成员函数声明为volatile
,则 它的类型是volatile X*
,并且如果声明了成员函数const volatile
,其类型为const volatile X*
.
In the body of a non-
static
([class.mfct]) member function, the keywordthis
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 classX
isX*
. If the member function is declared const, the type of this is constX*
, if the member function is declaredvolatile
, the type of this isvolatile X*
, and if the member function is declaredconst volatile
, the type of this isconst 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
说:>
对于两个类似的类型
T1
和T2
,类型T1
的prvalue可能是 使用const_cast
显式转换为类型T2
.这const_cast
的结果引用原始实体.
For two similar types
T1
andT2
, a prvalue of typeT1
may be explicitly converted to the typeT2
using aconst_cast
. The result of aconst_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屋!