Problem s gcc - moj, alebo kompilatora?

pavel hampl hampl na gncz.cz
Pátek Březen 16 15:28:35 CET 2001


Gratz Jaroslav wrote:
> 
> On 16 Mar 2001, Stanislav Meduna wrote:
> 
> > === statconst.c ===
> > static const char zeros1[4096];
> > ===
> >
> > $ gcc -c statconst.c # prekladane ako C je vsetko v poriadku
> > $ gcc -x c++ -c statconst.c # prelozit ako C++
> > statconst.c:1: uninitialized const `zeros1'
> 
> Pokud vim, tak tohle je korektni v C ale ne v C++, prekladac si stezuje po
> pravu. V gcc 2.95.2 to taky nejde prelozit.
> 
> Jaroslav Gratz
> 
Bruce Eckel, thinking in C++, kapitola o konstantách:
...
extern const int x = 1;

Notice that by giving it an initializer and saying it is extern, you
force storage to be created for the const (although the compiler still
has the option of doing constant folding here). The initialization
establishes this as a definition, not a declaration. The declaration:

     extern const int x;

in C++ means that the definition exists elsewhere (again, this is not
necessarily true in C). You can now see why C++ requires a const
definition to have an initializer: the initializer distinguishes a
declaration from a definition (in C it's always a definition, so no
initializer is
necessary). With an extern const declaration, the compiler cannot do
constant folding because it doesn't know the value.

PHampl
-- 
Pavel Hampl
GN Resources (CZ)                     tel: +420-2-22101821
Ve Smeckach 22                        email: hampl na gncz.cz
110 00 Praha 1                        http://www.gncz.cz
Czech Rep.


Další informace o konferenci Linux