gcc: why is "abcdef"[3] not a constant (error: initializer element is not constant)

Andrew Haley aph@redhat.com
Mon May 11 09:40:00 GMT 2015


Hi,

On 05/11/2015 09:02 AM, m-h-l wrote:

> sorry, it was not my intention to complain about gcc and I never said it's a
> defect.

OK, I too am sorry for being rude.  I thought your question rather
aggressive, which it clearly was not intended to be.  Mea culpa.  I
should not assume the worst.

> I thought this is a help forum and so I thought I can get a hint or
> trick how to work-around this. I really like gcc and you make a
> great job with it.
> 
> I just did not understand the idea behind this restriction. Perhaps
> there is a good reason (beside the standard) for this.

I don't think there is.  There has to be some limit on how far to do
constant propagation.  This limit is necessarily arbitrary: there is
no obvious point at which to say "this far, and no further."

The need for a limit is especially true when the compiler is at -O0,
with all optimizations turned off.  There have to be rules about what
expressions must be evaluated as constant expressions because we can
use a constant expression as a key in a switch statement.  Is it
reasonable, then, to expect a simple C compiler always to look through
all complex expressions at compile time?  Where would you stop?

The point I'm making is this: there is no one true answer.  The
standard makes a cut.

Andrew.



More information about the Gcc-help mailing list