Anshul Sawant wrote in
news:bd9cdcf.0402230532.562e3292@posting.google.co m:
[color=blue]
> Have a look at the following code:
>
> #include<iostream>
>
> using namespace std;
>
> int main()
> {
> const int a = 1;
> const int* const aptr1 = &a;[/color]
This cast if fine and legal (ok you missed a '('), but ...
[color=blue]
> int* const aptr2 = const_cast<int*>aptr1);[/color]
The use of the result const_cast here is UB (uderfined Behaviour),
you should only use const_cast when you *know* the original thing
pointed to wasn't a constant.
[color=blue]
> *aptr2 = 2;[/color]
All bets are now Officialy (according to The C++ Standard) off.
[color=blue]
> cout<<a<<endl;
> cout<<*(&a)<<endl;
> cout<<*aptr1<<endl;
> cout<<*aptr2<<endl;
> }
>
> Output is (for g++ (ver. 3.2) and visual C++ 6 compiler)
>
> 1
> 1
> 2
> 2
>
> Is it the correct behaviour?[/color]
Yup, but so is you computer growing leggs and wandering off to
make a cup of tea.
[color=blue]
> Is it due to constant folding?[/color]
Its due to underfined behaviour.
[color=blue]
> If yes, how is constant folding possible when we are referencing the
> variable?[/color]
HTH.
Rob.
--
http://www.victim-prime.dsl.pipex.com/