Not. According to ยง5.4.4 of the C ++ standard, castings that can be performed by a C-style application are as follows:
โ a const_cast (5.2.11), โ a static_cast (5.2.9), โ a static_cast followed by a const_cast, โ a reinterpret_cast (5.2.10), or โ a reinterpret_cast followed by a const_cast
This is commonly known as "dropping const -ness," and the compiler will be inconsistent with that part of the standard if it does not compile this code.
As ildjarn points out, modifying a const object by discarding const ness is undefined behavior. This program does not demonstrate undefined behavior, because although the object pointed to by a pointer to const , the object itself is not const (thanks R. Martigno and eharvest for correcting my poor reading).
Seth carnegie
source share