C++17 Standard – Cast away const of static

  • A+
Category:Languages

Recently I decided to dive into the C++ Standard and check whether certain code snippets are well defined and where to find those definitions in the standard. Since the standard is rather hard to get right (especially if you are not used to it) I wanted to verify if my assumption is right.

I came across the following example (which is obviously a bad idea). It compiles just fine (using g++ 8.2.1) but SEGFAULTs during execution:

#include <iostream>  static const int staticInt = 23;  int main () {     int &localInt = const_cast<int &>(staticInt);     localInt = 11;     std::cout << staticInt << std::endl;     return 0; } 

So, I searched through the standard (I use the working draft on open-std btw) and found paragraph 6.8.10:

Creating a new object within the storage that a const complete object with static, thread, or automatic storage duration occupies, or within the storage that such a const object used to occupy before its lifetime ended, results in undefined behavior.

Am I right, that this paragraph is applicable for the given example? If I am not, where else should I look at?

 


This is undefined behavior because of the attempt to modify a const variable after using a const_cast on it.

Citations from n4659, the final working draft of C++17 . The relevant passage in this case is:

8.2.11 Const cast [expr.const.cast]
...
6 [ 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. —end note ]

Also of relevance is this section related to const objects:

10.1.7.1 The cv-qualifiers [dcl.type.cv]
...
4 Except that any class member declared mutable can be modified, any attempt to modify a const object during its lifetime results in undefined behavior.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: