Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
polarlinda6 opened this issue Sep 4, 2024 · 2 comments
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" duplicate Resolved as duplicate

Comments

@polarlinda6
Copy link

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.

@github-actions github-actions bot added the clang Clang issues not falling into any other category label Sep 4, 2024
@MitalAshok
Copy link
Contributor

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

@EugeneZelenko EugeneZelenko added duplicate Resolved as duplicate clang:frontend Language frontend issues, e.g. anything involving "Sema" and removed clang Clang issues not falling into any other category labels Sep 4, 2024
@EugeneZelenko EugeneZelenko closed this as not planned Won't fix, can't repro, duplicate, stale Sep 4, 2024
@llvmbot
Copy link
Member

llvmbot commented Sep 4, 2024

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

Additional Information:

Version: clang version 18.1.8
Link: godbolt

PS: The code compiles successfully on gcc and msvc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" duplicate Resolved as duplicate
Projects
None yet
Development

No branches or pull requests

4 participants