Lior Kogan
Lior Kogan

Reputation: 20658

const values run-time evaluation

The output of the following code:

const int i= 1;
(int&)i= 2;          // or:  const_cast< int&>(i)= 2;
cout << i << endl;

is 1 (at least under VS2012)

My question:

Upvotes: 3

Views: 201

Answers (6)

jogojapan
jogojapan

Reputation: 70017

As said by others, the behaviour is undefined.

For the sake of completeness, here is the quote from the Standard:

(§7.1.6.1/4) Except that any class member declared mutable (7.1.1) can be modified, any attempt to modify a const object during its lifetime (3.8) results in undefined behavior. [ Example:

[...]

const int* ciq = new const int (3);  // initialized as required
int* iq = const_cast<int*>(ciq);     // cast required
*iq = 4;                             // undefined: modifies a const object

]

Note that the word object is this paragraph refers to all kinds of objects, including simple integers, as shown in the example – not only class objects.

Although the example refers to a pointer to an object with dynamic storage, the text of the paragraph makes it clear that this applies to references to objects with automatic storage as well.

Upvotes: 3

Stephane Rolland
Stephane Rolland

Reputation: 39916

You are doing a const_cast using the C-like cast operator.

Using const_cast is not guaranteeing any behaviour.

if ever you do it, it might work or it might not work.

(It's not good practice to use C-like operators in C++ you know)

Upvotes: -1

Eric Postpischil
Eric Postpischil

Reputation: 223747

Is this behavior defined?

The behavior of this code is not defined by the C++ standard, because it attempts to modify a const object.

Would the compiler always use the defined value for constants?

What value the compiler uses in cases like this depends on the implementation. The C++ standard does not impose a requirement.

Is it possible to construct an example where the compiler would use the value of the latest assignment?

There might be cases where the compiler does modify the value and use it, but they would not be reliable.

Upvotes: 4

user1284631
user1284631

Reputation: 4616

The answer is that the behavior is undefined.

I managed to set up this conclusive example:

#include <iostream>

using namespace std;

int main(){

        const int i = 1;

        int *p=const_cast<int *>(&i);
        *p = 2;
        cout << i << endl;

        cout << *p << endl;

        cout << &i << endl;

        cout << p << endl;

        return 0;
}

which, under gcc 4.7.2 gives:

1
2
0x7fffa9b7ddf4
0x7fffa9b7ddf4

So, it is like you have the same memory address as it is holding two different values.

The most probable explanation is that the compiler simply replaces constant values with their literal values.

Upvotes: 3

StackHeapCollision
StackHeapCollision

Reputation: 1833

Yes you can, but only if you initiate a const as a read-only but not compile-time const, as follows:

int y=1;
const int i= y;
(int&)i= 2;
cout << i << endl; // prints 2

C++ const keyword can be missleading, it's either a const or a read-only.

Upvotes: -2

Bo Persson
Bo Persson

Reputation: 92341

It is totally undefined. You just cannot change the value of constants.

It so happens that the compiler transforms your code into something like

cout << 1 << endl;

but the program could just as well crash, or do something else.

If you set the warnings level high enough, the compiler will surely tell you that it is not going to work.

Upvotes: 7

Related Questions