c++/10634: in-class initialization of static data members no longer allowed

gonz@ratloop.com gonz@ratloop.com
Tue May 6 01:06:00 GMT 2003


>Number:         10634
>Category:       c++
>Synopsis:       in-class initialization of static data members no longer allowed
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Tue May 06 01:06:00 UTC 2003
>Closed-Date:
>Last-Modified:
>Originator:     Pete Gonzalez
>Release:        gcc-3.2.3 and 3.3 (20030428 prerelease)
>Organization:
>Environment:
Debian Linux (testing - "sarge")
>Description:
The following code compiled without problems under GCC 3.2.1 and GCC 2.95.4:

struct Struct {
  static unsigned char *const pointer = (unsigned char*)0x123;
};

However, with GCC version 3.2.3 and 3.3 it produces the error message "invalid in-class initialization of static data member of non-integral type `unsigned char* const'".

The code can be made to compile by moving the definition into the .cpp file; however, this is suboptimal because the constant can no longer be inlined.

This error message is coming from check_static_variable_definition() in gcc/cp/decl.c.  The comment there reads:

  /* Motion 10 at San Diego: If a static const integral data member is
     initialized with an integral constant expression, the initializer
     may appear either in the declaration (within the class), or in
     the definition, but not both.  If it appears in the class, the
     member is a member constant.  The file-scope definition is always
     required.  */

Since the "but not both" condition is not applicable in the above example, this looks like a bug.
>How-To-Repeat:
Copy the above code into test.cpp and then run "gcc -c test.cpp"
>Fix:
In gcc/cp/decl.c, replace "if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE)" with "if (false)"  :-)
>Release-Note:
>Audit-Trail:
>Unformatted:



More information about the Gcc-bugs mailing list