Re: MSDN const_cast sample




"George" <George@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:DCC0E52E-4BBC-46F8-BEC0-6B552C760335@xxxxxxxxxxxxxxxx
Hi Ben,


I have used Visual Studio 2008 tested your code works. The output is 425.

Under debug or release? Which optimizations enabled, if release?


I do not quite understand what do you mean,

I see no reason to believe that the compiler won't use the register used
to
hold j instead of reading memory again. For that matter, it is probably
allowable to delay assigning a value to j until after the call to mutate.

What do you mean *won't use the register* and *delay assigning*? Is it
some
special approaches to do optimization? Could you provide more descriptions
please?


regards,
George

"Ben Voigt [C++ MVP]" wrote:

Otherwise, a compiler is non-compliant if casting away const doesn't
work.
Assuming this code:

void mutate( int const& i) {
const_cast<int&>(i) = 42;
}

int i = 5;
mutate(i);
std::cout << i << std::endl;

I would assume the compiler to behave properly and output 42. Do you
know
of
any compilers that don't?

It should. It is only undefined behaviour if the variable/object being
modified was originally created as const. Here, i is not. And hence,
the modification should work. Don't know of a compiler that would go
against the above though.

I don't think "originally created as non-const" is sufficient.

For example:

void mutate( int const& i) {
const_cast<int&>(i) = 42;
}

void f(const int* const p)
{
int j = *p;
mutate(*p);
std::cout << *p << j << std::endl;
}

int i = 5;
f(&i);

I see no reason to believe that the compiler won't use the register used
to
hold j instead of reading memory again. For that matter, it is probably
allowable to delay assigning a value to j until after the call to mutate.





.



Relevant Pages

  • Re: MSDN const_cast sample
    ... void mutate(int const& i) { ... I would assume the compiler to behave properly and output 42. ...
    (microsoft.public.vc.language)
  • Re: MSDN const_cast sample
    ... void mutate(int const& i) { ... I would assume the compiler to behave properly and output 42. ...
    (microsoft.public.vc.language)
  • Re: It Pays to Enrich Your C Skills
    ... Check if you can score a perfect 10 (without using a compiler). ... int main{ ... struct bitfield { ... out if it is a negative integer constant or a constant expression ...
    (comp.lang.c.moderated)
  • OT: Re: Perl Peeves
    ... I see the result of a test being used as an int. ... the compiler just assumed you knew what you were doing ... introduced to the language later, so void * was unheard of in most code. ... This didn't mean bool was special, declaring it just signaled to the ...
    (comp.lang.perl.misc)
  • Re: OT: Re: Perl Peeves
    ... when I see the result of a test being used as an int. ... compiler just assumed you knew what you were doing and would ... This didn't mean bool was special, declaring it just signaled to the ... What "normalization of bool results is built into the compiler"? ...
    (comp.lang.perl.misc)