Title
Diagnosing ODR violations for static data members
Status
review
Section
11.4.9.3 [class.static.data]
Submitter
Mike Miller

Created on 2013-07-31.00:00:00 last changed 2 weeks ago

Messages

Date: 2024-12-15.00:00:00

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.
Date: 2024-03-15.00:00:00

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.
Date: 2022-09-25.18:08:42

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.”

History
Date User Action Args
2024-12-07 21:35:20adminsetmessages: + msg7915
2024-03-06 21:18:13adminsetmessages: + msg7633
2024-03-06 21:18:13adminsetstatus: drafting -> review
2013-10-14 00:00:00adminsetstatus: open -> drafting
2013-07-31 00:00:00admincreate