This is an unofficial snapshot of the ISO/IEC JTC1 SC22 WG21 Core Issues List revision 116a. See http://www.open-std.org/jtc1/sc22/wg21/ for the official list.

2024-12-19


1721. Diagnosing ODR violations for static data members

Section: 11.4.9.3  [class.static.data]     Status: review     Submitter: Mike Miller     Date: 2013-07-31

Describing the handling of static data members with brace-or-equal-initializers, 11.4.9.3 [class.static.data] paragraph 4 says,

The member shall still be defined in a namespace scope if it is odr-used (6.3 [basic.def.odr]) in the program and the namespace scope definition shall not contain an initializer.

The word “shall” implies a required diagnostic, but this is describing an ODR violation (the static data member might be defined in a different translation unit) and thus should be “no diagnostic required.”

Additional notes (March, 2024)

This paragraph has more issues and is largely redundant with 6.2 [basic.def] paragraph 2.3 and paragraph 2.4.

Possible resolution [SUPERSEDED]:

Change 11.4.9.3 [class.static.data] paragraph 4 as follows:

If a non-volatile non-inline const static data member is of integral or enumeration type, its declaration in the class definition can specify a brace-or-equal-initializer in which every initializer-clause that is an assignment-expression is a constant expression (7.7 [expr.const]). The member shall still be defined in a namespace scope if it is odr-used (6.3 [basic.def.odr]) in the program and the namespace scope definition shall not contain an initializer . The declaration of an inline static data member (which is a definition) may specify a brace-or-equal-initializer . If the member is declared with the constexpr specifier, it may be redeclared in namespace scope with no initializer (this usage is deprecated; see D.7 [depr.static.constexpr]). Declarations of other static data members shall not specify a brace-or-equal-initializer.
If a declaration of a non-inline static data member in a class definition specifies a brace-or-equal-initializer, the member shall be of non-volatile const integral or enumeration type and shall be constant-initialized (7.7 [expr.const]), and the definition (if any) shall not specify a brace-or-equal-initializer.

Additional notes (December, 2024)

9.4.1 [dcl.init.general] paragraph 2 and paragraph 3 serve no normative purpose and should be struck.

Possible resolution:

  1. Remove 9.4.1 [dcl.init.general] paragraph 2 and paragraph 3:

    Except for objects declared with the constexpr specifier, for which see 9.2.6 [dcl.constexpr], an initializer in the definition of a variable can consist of arbitrary expressions involving literals and previously declared variables and functions, regardless of the variable's storage duration. [Example 1: ... —end example]

    [Note 2: Default arguments are more restricted; see 9.3.4.7 [dcl.fct.default]. —end note]

  2. Change 11.4.9.3 [class.static.data] paragraph 4 as follows:

    If a non-volatile non-inline const static data member is of integral or enumeration type, its declaration in the class definition can specify a brace-or-equal-initializer in which every initializer-clause that is an assignment-expression is a constant expression (7.7 [expr.const]). The member shall still be defined in a namespace scope if it is odr-used (6.3 [basic.def.odr]) in the program and the namespace scope definition shall not contain an initializer . The declaration of an inline static data member (which is a definition) may specify a brace-or-equal-initializer . If the member is declared with the constexpr specifier, it may be redeclared in namespace scope with no initializer (this usage is deprecated; see D.7 [depr.static.constexpr]). Declarations of other static data members shall not specify a brace-or-equal-initializer.
    If a declaration of a non-inline static data member in a class definition specifies a brace-or-equal-initializer, the member shall be of non-volatile const integral or enumeration type and shall be constant-initialized (7.7 [expr.const]), and the definition (if any) shall not specify a brace-or-equal-initializer.