This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [C++0x] code that used to be accepted isn't accepted anymore (related to constexpr changes?)


My turn to ask a question: I would like to understand how what I wrote
in my first email "makes no sense" :-)

This code:

>>>> static const unsigned x = (unsigned)-1;
>>>>
>>>> enum e {
>>>> ? ? ? ?ey = (int)x
>>>> };

relies on the constant global variable x being a true compile-time
constant, right?
But as far as I can see, it is impossible to guarantee that it
actually is, since in theory you could do

    const_cast<int*>(&x) = 0;

In practice, this code compiles and only crashes at runtime as an
access violation (writing to a read-only segment). But it certainly
doesn't error at compile time; and even the fact that it crashes at
runtime is obviously platform-dependent (it wouldn't crash on MS-DOS
:-) ).

I guess that the only way is to say that the compiler remembers the
initializer value -1 for x and uses it instead of x itself. In other
words x itself is never used. I guess that this is where the "static"
matters here since inside of one translation unit the initializer
value -1 can be remembered?

Thanks
Benoit


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]