llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.83k stars 11.91k forks source link

[Clang][C++] It is not possible to initialize constants using the 'constexpr' static member of a function reference parameter #107203

Closed PolarLinda6 closed 2 months ago

PolarLinda6 commented 2 months ago
struct A {
  constexpr static int value = 0;
};

void func(A& _) {
  constexpr auto result = _.value;
}

auto main() -> int {
  A a;
  func(a);
}

error message:

<source>:6:18: error: constexpr variable 'result' must be initialized by a constant expression
    6 |   constexpr auto result = _.value;
      |                  ^        ~~~~~~~
<source>:6:27: note: function parameter '_' with unknown value cannot be used in a constant expression
    6 |   constexpr auto result = _.value;
      |                           ^
<source>:5:14: note: declared here
    5 | void func(A& _) {
      |              ^

Additional Information:

Version: clang version 18.1.8 Link: godbolt

PS: The code compiles successfully on gcc and msvc.

MitalAshok commented 2 months ago

This is P2280R4 which Clang doesn't implement yet. It's tracked by #63139

llvmbot commented 2 months ago

@llvm/issue-subscribers-clang-frontend

Author: PolarLinda6 (PolarLinda6)

```cpp struct A { constexpr static int value = 0; }; void func(A& _) { constexpr auto result = _.value; } auto main() -> int { A a; func(a); } ``` #### error message: ```txt <source>:6:18: error: constexpr variable 'result' must be initialized by a constant expression 6 | constexpr auto result = _.value; | ^ ~~~~~~~ <source>:6:27: note: function parameter '_' with unknown value cannot be used in a constant expression 6 | constexpr auto result = _.value; | ^ <source>:5:14: note: declared here 5 | void func(A& _) { | ^ ``` #### Additional Information: Version: clang version 18.1.8 Link: [godbolt](https://godbolt.org/z/xYWfx48sT) PS: The code compiles successfully on gcc and msvc.