Skip to content

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

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

Activity

  1. jensmaurer commented on Feb 17, 2022

    @jensmaurer
    Member
  2. jensmaurer commented on Feb 18, 2022

    @jensmaurer
    Member

    @opensdh, please check: CWG2531

  3. changed the title [-][dcl.constexpr] Static data members redeclared as constexpr[/-] [+]CWG2531 [dcl.constexpr] Static data members redeclared as constexpr[/+] on Feb 19, 2022
  4. opensdh commented on Feb 21, 2022

    @opensdh
    Author

    It looks correct; presumably you omitted the implementation behavior purposefully.

  5. jensmaurer commented on Feb 21, 2022

    @jensmaurer
    Member

    Yeah, I'm not sure it adds much, given that we want to outlaw the situation regardless.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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