Strange behavior of const_cast [duplicate]
Because modifying a variable declared to be const is undefined behavior, literally anything can happen.
Because modifying a variable declared to be const is undefined behavior, literally anything can happen.
As-written the way you’re doing this is undefined behavior. If you wanted to see the effects of const_cast<> in a defined manner: int a = 5; // note: not const. regular object. const int& cref = a; // const-reference to same object. cref = 7; // illegal. cref is a const reference. const_cast<int&>(cref) = 7; … Read more
Because you’re thwarting the purpose of const, which is to keep you from modifying the argument. So if you cast away the constness of something, it’s pointless and bloating your code, and it lets you break promises that you made to the user of the function that you won’t modify the argument. In addition, using … Read more
The language allows implicit conversion from double ** to const double *const *, but not to const double **. The conversion you attempt would implicitly violate the rules of const correctness, even though it is not immediately obvious. The example in the [de-facto standard] C++ FAQ illustrates the issue https://isocpp.org/wiki/faq/const-correctness#constptrptr-conversion Basically, the rule is: once … Read more
Yes. This is entirely legal. (It is dangerous, but it is legal.) If you (attempt to) modify a an object declared const, then the behaviour is undefined. From n4659 (which is the last draft of C++17), section 10.1.7.1 [dcl.type.cv] para 4: Except that any class member declared mutable (10.1.1) can be modified, any attempt to … Read more
it is pretty much designed to be only used with legacy APIs that are not const correct i.e. with a function you can’t change that has non const interface but doesn’t actually mutate anything on the interface
Quote from cppreference: Even though const_cast may remove constness or volatility from any pointer or reference, using the resulting pointer or reference to write to an object that was declared const or to access an object that was declared volatile invokes undefined behavior. So yes, modifying constant variables is undefined behavior. The output you see … Read more
You are not allowed to const_cast variables that are actually const. This results in undefined behavior. const_cast is used to remove the const-ness from references and pointers that ultimately refer to something that is not const. So, this is allowed: int i = 0; const int& ref = i; const int* ptr = &i; const_cast<int&>(ref) … Read more
const_cast is safe only if you’re casting a variable that was originally non-const. For example, if you have a function that takes a parameter of a const char *, and you pass in a modifiable char *, it’s safe to const_cast that parameter back to a char * and modify it. However, if the original … Read more
The key of the set is declared as being a pointer-to-int, an int*. But this: void push( const B& b ) { set.insert( &b.x ); } is passing the address of a constant int, an int const*, hence the compiler error. Removing the const from the argument would resolve the compiler error, as would making … Read more