vendredi 18 mai 2018

Are there any limitations on adding const to the topmost level of a pointer?

I am aware that this is not allowed for lower levels of a pointer.

Case in point (of which I am fairly certain that it is allowed, despite everyone claiming that pointers to void may only be cast back to the same type):

Class c;
void *vp = &c;
Class const *cp = static_cast<Class const *>(vp);
assert(cp == &c);

In the same vein, is storing a pointer to non-const void allowed if cast back to pointer to constant:

Class const cc;
void *vp = const_cast<Class *>(&cc);
Class const *ccp = static_cast<Class const *>(vp);
assert(ccp == &cc);

Are both of these use cases fully defined by the standard with the expected behavior?

Aucun commentaire:

Enregistrer un commentaire