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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 11/07/2010 05:29 AM, Benoit Jacob wrote:
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;

The standard says,

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.

So the above assignment has undefined behavior, which means the compiler is allowed to assume that it doesn't happen.

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.

Right.

I guess that this is where the "static"
matters here since inside of one translation unit the initializer
value -1 can be remembered?

'static' doesn't matter, just that it's const, integral, and initialized with a constant expression.

Jason


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux