Skip to content

CWG2531 [dcl.constexpr] Static data members redeclared as constexpr #1

@opensdh

Description

@opensdh

Reference (section label)

[dcl.constexpr]/1

Issue description

C++17 made constexpr static data members implicitly inline:

A function or static data member declared with the constexpr or consteval specifier is implicitly an inline function or variable ([dcl.inline]).

However, that makes the following well-formed C++14 program IFNDR per [dcl.inline]/5:

// x.hh
struct X {
  static const int x;
};
// TU 1
#include"x.hh"
constexpr int X::x{};
// TU 2
#include"x.hh"
int main() {return !&X::x;}

Current GCC and Clang do treat X::x as a weak symbol, but since they emit it even without an odr-use in the TU programs like this do work in practice.

Suggested resolution

Change [dcl.constexpr]/1 as follows:

[…] A function or static data member declared with the constexpr or consteval specifier on its first declaration is implicitly an inline function or variable ([dcl.inline]). […]

(Functions must be declared constexpr on every declaration if on any, so this isn't a change for them.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions