The reason you can change the value is because you made an order such as a pointer that removed const ness:
int *p = (int *)&a;
This method outputs a const int* (namely &a ) to int * , which allows you to freely change the variable. Typically, the compiler warns you about this, but an explicit output type suppresses the warning.
The basic rationale for const is to prevent you from accidentally changing what you promised not to do. This, as you saw, is not inviolable, and you can discard const with impunity, just as you can do other unsafe things, such as converting pointers to integers or vice versa. The idea is that you should try your best not to get confused with const , and the compiler will warn you if you do this. Of course, the addition to the cast tells the compiler “I know what I'm doing,” and so in your case the above does not generate any warnings.
templatetypedef
source share