3

Possible Duplicate:
Is const_cast safe?

Obviously I'd never write this code, but it's a very much simpler example of something that came up in a real program.

#include <iostream>

void change(const int& data)
{
    int& data2 = const_cast<int&>(data);
    data2 = 100;
}

int main()
{
    int thing = 123;
    change(thing);

    std::cout << thing << "\n";
}

Is it well defined behavior that this alters the data referred to, or is the compiler allowed to assume that because it's passing a const int& that the function can't change the value passed in and generate code accordingly?

edit: All the compilers I tried it on output the changed value, 100.

This appears to be a duplicate of Can C++ compiler assume a const bool & value will not change? so I'm happy to close this one.

Community
  • 1
  • 1
jcoder
  • 29,554
  • 19
  • 87
  • 130
  • That question talks about removing const from data which was originally const which isn't the case here. Unless I missed some detail of that question, which is possible :) – jcoder Oct 07 '12 at 20:00
  • Read the first answer to that question, as well as the last comment on that answer. It directly applies to this question. – cdhowie Oct 07 '12 at 20:01
  • I'm not sure it applies. My question is can the compiler when it generates the code for "main" assume that when it calls "change", the value of the paramater can't be changed as it's const. I think this is a slightly different question. – jcoder Oct 07 '12 at 20:03
  • Your question title and description seem slightly different than what you just stated. You may consider opening a new question that asks what you're really interested in. "Is this well-defined behavior" -- the answer is yes, as given by the linked question. If your question is whether the compiler will try to avoid re-reading the `thing` local, then ask that question. – cdhowie Oct 07 '12 at 20:06
  • yes, sorry if it's unclear. I will clarify my question. Should I ask a new question or edit this one? – jcoder Oct 07 '12 at 20:06
  • 2
    Actually someone linked http://stackoverflow.com/questions/5128639/can-c-compiler-assume-a-const-bool-value-will-not-change which does seem to answer this, although they seem to have removed the comment. In which case I'm happy for it to be removed as a duplicate question. – jcoder Oct 07 '12 at 20:09

1 Answers1

0

This is not well defined behavior unless you know if the original value was constant. It will work in the exact example you have given. However, if you passed in a const variable it could cause a memory access violation. This would be because the compiler put the const variable on a read-only memory page.

CrazyCasta
  • 26,917
  • 4
  • 45
  • 72
  • I understand that, and that's not my question as such. My question is, is the compiler allowed to assume when generating the code for "main" that the function can't change the value, and generate code that does not see the update. I think that's a different question – jcoder Oct 07 '12 at 20:01