Re: compiler error with struct member initializers
- From: "Antoine Leca" <root@xxxxxxxxxxxxxxxxx>
- Date: Wed, 23 Nov 2005 15:16:05 +0100
En news:dm001s$kav$1@xxxxxxxxx, Andreas Martin va escriure:
> Recently I had to code this:
>
> TBBUTTON tbb = {0, 0, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0L,
> (int)"Title"};
>
> but that produces a compiler error "initializer must be constant" for
> (int)"test". But that _is_ constant.
I am not sure.
The C Standard requires (6.5.7/6.7.8) that
All the expressions in an initializer for an object that
has static storage duration shall be constant expressions
or string literals.
This is a constraint, so possible violations should raise a diagnostic (I do
not have the book at hand, but I feel it is explained what this means; else,
you have plently of material this about on the web.)
It is my understanding that the above message is the corresponding
diagnostic, where "constant" in the mesage should stand for /constant
expression/ in the context of the standard.
It is not the place here to repeat the whole 6.4/6.6 "Constant Expressions"
subclause from the standard, so suffice to say I did not see any possibility
of a cast to int of a pointer (even a "constant" pointer like a string
litteral) to be a constant expression; that is, unless someone uses the "An
implementation may accept other forms of constant expressions." blank-carpet
paragraph.
> A workaround:
> const int wa = (int)"Title";
Now it is interesting! I fail to understand why this does _not_ raise the
same warning/error. On the other side, neither is GNU GCC (tried 3.4.2)...
Food for thought.
Antoine
.
- References:
- compiler error with struct member initializers
- From: Andreas Martin
- compiler error with struct member initializers
- Prev by Date: Re: compiler error with struct member initializers
- Next by Date: Re: compiler error with struct member initializers
- Previous by thread: Re: compiler error with struct member initializers
- Next by thread: Re: compiler error with struct member initializers
- Index(es):