Issue 126558
Summary [Clang] Cannot use a static constexpr/consteval member function to initialize a static constexpr member
Labels
Assignees
Reporter YexuanXiao
    I've noticed that consteval member functions exhibit very strange behavior—they can initialize static non-constexpr members but fail to work for static constexpr members.
Example ([Godbolt](https://godbolt.org/z/Ynn5xf78s)):
```cpp
struct A
{
    static inline constexpr bool test_constexpr() noexcept
    {
        return true;
    }
    static inline constexpr bool t_expr = test_constexpr(); // can't compile
    static inline constexpr bool test_consteval() noexcept
    {
        return true;
    }
    static inline constexpr bool t_eval = test_consteval();  // can't compile
    static inline bool t_non_const = test_constexpr();
};
inline constexpr bool t_expr = A::test_constexpr();
inline constexpr bool t_eval = A::test_consteval();
```
Output:
```cpp
<source>:8:34: error: constexpr variable 't_expr' must be initialized by a constant _expression_
    8 |     static inline constexpr bool t_expr = test_constexpr();
      |                                  ^        ~~~~~~~~~~~~~~~~
<source>:8:43: note: undefined function 'test_constexpr' cannot be used in a constant _expression_
    8 |     static inline constexpr bool t_expr = test_constexpr();
      |                                           ^
<source>:3:34: note: declared here
    3 |     static inline constexpr bool test_constexpr() noexcept
      |                                  ^
<source>:15:34: error: constexpr variable 't_eval' must be initialized by a constant _expression_
   15 |     static inline constexpr bool t_eval = test_consteval();
      |                                  ^        ~~~~~~~~~~~~~~~~
<source>:15:43: note: undefined function 'test_consteval' cannot be used in a constant _expression_
   15 |     static inline constexpr bool t_eval = test_consteval();
      |                                           ^
<source>:10:34: note: declared here
   10 |     static inline constexpr bool test_consteval() noexcept
      |                                  ^
2 errors generated.
Compiler returned: 1
```
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to