This is the mail archive of the gcc-bugs@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]

Re: c++/3800: A value of const static class attribute cannot be usedin STL



If I define any constant static member array|struct with value inside
class body, the compiler emits an error. The error is not emited for
constant static int and unsigned (and probably all the simple types as
char, ...). If, according to the standard, it is not allowed to define (=
together with its value)  simple const static members inside class body,
the compiler should emit an error, too.

But I don't think it is not allowed. For simple const types (as the
opposite of arrays) the linker musn't link the value to the resulting
binary, it is more effective (and at least in gcc 2.95.2 also done) when
the compiler fills in the value on all places it is referenced to. I hope
the compiler also does simple arithmetics with constants instead of
generating code, which would compute it.

If you still not believe in my argues, try this:

---

#include <stdio.h>

class C {
  public:
    const static int i = 7;
};

int main(int argc, char *argv[]) {
  printf("%i\n", C::i);
  return 0;
}

---

This way, it is all right. It you change "int i=7" to some kind of struct,
compiler emits "invalid in-class initialization of static data member of
non-integral type '...'", which also probably means, that integral types
(in my words 'simple' types) are allowed to be initialized in-class.

I have investigated HALF A DAY to locate the bug and make a short sample
which demonstrates it. It is from quite a big project I am working on, it
uses templates and various macro preprocessing extensively, g++ compiles
well it all (except the bug).

------------------------------------------------------------------------
The problem is somewhere in gcc's template instantiation, or, with less
probability, in STL (STL didn't change much from gcc-2.95 to gcc-3.0).
------------------------------------------------------------------------

Milan Krcmar



On 26 Jul 2001 aoliva@gcc.gnu.org wrote:

> Synopsis: A value of const static class attribute cannot be used in STL
>
> State-Changed-From-To: open->closed
> State-Changed-By: aoliva
> State-Changed-When: Thu Jul 26 15:29:32 2001
> State-Changed-Why:
>     Not a bug.  Every static data member must be defined outside the class body, even if it is a constant.
>
> http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view&pr=3800&database=gcc
>


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