Is there a way to change a cast off a const

I want to take const variable cast off the const give it a new value and make it const again. Can I do that?

1
2
3
4
5
6
7
8
void f() {
    const std::string s = "wilson"; //promise not to change value
    
    s = "not wilson"; //now I changed my mind, but I want the new value to be const

    std::cout << s << '\n';
}
Last edited on
No. You made a promise and the compiler will probably optimize code to make full use of that promise.


However, your sample program is too trivial to make a difference. You must have some more real case in mind.

Besides, you can perhaps refactor the code:
1
2
3
4
5
6
7
8
void g(const std::string&);
void h(const std::string&);
void f() {
    std::string s = "wilson";
    g(s); // will use s like it was const
    s = "not wilson";
    h(s); // will use s like it was const
}
Yes, I already found a work around. This is it:
 
const std::string dir = argc == 3 ? argv[2] : ".";

But I could swear that I read somewhere that you can cast off const.

What does const_cast do if it doesn't cast off const?
Last edited on
yes, you can cast away const, and also private.

in both cases it can be (always is for private) 'undefined behavior' that may or may not work, and if it does work, a recompile in the future may or may not work then. Its a terrible thing to do (looking at point 3 below scenarios here). Its just pointer magic -- the compiler watches variables, but not memory locations, so if you can get a non const pointer to a const memory location you can mess with it; except that when optimizing the compiler MAY put the constant value you typed in the code into the machine language, so there IS NO real address to modify, so your changes could {work, not work, mix of the two} in various places in the code. For this to work, the constant must have a memory location to begin with, which some do not. Turning off optimize may do the trick, but who would ever do that; its like 2-3 times slower on average.


https://www.geeksforgeeks.org/const_cast-in-c-type-casting-operators/
read this and pay attention to point # 3.

or to confusingly TLDR / executive summary:
const cast removes const from things that were NOT constant at declare but became constant at a later time, restoring their original mutability. Things that were constant at declare can only be changed with unsafe hackery.

I don't think I have ever needed const cast, but I am not sure that where I worked we followed strict const correctness concepts either. If you do that religiously, you will need const cast now and then to bypass an unnecessary/excessive const somewhere in your code.
Last edited on
It does indeed remove const, although you are still not allowed to modify a const object.

It exists so that you can call const-incorrect functions. It is sometimes used that way, and sometimes used to avoid code duplication between functions overloaded on the cv-qualification of an argument.

1
2
3
4
5
6
7
8
9
10
// bad practice: const-incorrect function
// the referent is not modified, so this is well-formed code.
void f(int &x) {}

int main()
{
    int const a = 0;
    f(const_cast<int&>(a)); // okay
    // const_cast<int&>(a) = 10; // wrong: modifies object declared const
}

Last edited on
Thank you, I think I understand but I'm going to have to do some more reading.
Topic archived. No new replies allowed.