c++ - Is this const_cast undefined behavior? -
i wondering whether following undefined behavior
// case 1: int *p = 0; int const *q = *const_cast<int const* const*>(&p); // case 2: (i think same) int *p = 0; int const *const *pp = &p; int const *q = *pp; is undefined behavior reading int* if int const*? think undefined behavior, thought adding const in general safe, i'm unsure.
qualification-wise, it's fine. each expression split statement:
int *p = 0; // ok int **addrp = &p; // ok int const *const *caddrq = addrp; // ok, qualification conv. according §4.4/4 int const *q = *caddrq; // ok note rules of const_cast (§5.2.11/3) identical of qualification conversion, without requirement of being monotonically increasing in qualification. in case, because you're ever adding qualifications const_cast unnecessary.
concerning aliasing, don't think it's issue, here, or @ least it's not intended be.
like mentioned, there's new bullet in c++0x list of allowed access methods (§3.10) allows similar types ("similar" being types arising qualification conversions). in c++03 bullet missing, suspect bullet allowing more cv-qualified access meant cover that, technically isn't (that is, commitee overlooked this).
Comments
Post a Comment