This is the mail archive of the gcc@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: Constant initializers


On 18 October 2010 22:04, Matt Fischer wrote:
> I'm attempting to port some code to gcc, and in a couple of places
> it's using a construct that it doesn't like. ?A simplified example is
> the following (this is in global scope):
>
> static const int A = 1;
> static const int B = A;
>
> This compiles fine with g++, but gcc says "error: initializer element
> is not constant". ?The compiler this code used to use handles it fine,
> and given that it's also legal in C++, I was wondering if anybody
> could comment on the (il)legality of this construct. ?Does the C spec
> simply say that one can never use a variable to initialize another
> global variable, regardless of whether or not it's constant? ?Is there
> some way to convince gcc to accept this syntax? ?I tried various
> values for --std and couldn't seem to find any that worked.

This question is more suitable for the gcc-help mailing list, this
list is about development *of* gcc, not development *with* gcc. Please
take any further questions to gcc-help.

The rules on what is allwoed in constant expressions differs between C
and C++. Specifically, C++ allow const variables and static data
members in a constant expression if they are initialized with a
constant expression.  In your example A is initialized with a constant
expression, so A itself can be used in a constant expression in C++,
but not in C.


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