| Issue |
237943
|
| Summary |
[clang] Immediate invocation in a default member initializer of a class template is evaluated at instantiation, not at the point of use
|
| Labels |
|
| Assignees |
|
| Reporter |
kstolze-ocient
|
Clang evaluates an immediate invocation in a default member initializer of a class template when it instantiates the initializer. C++23 forbids that evaluation and requires it where a constructor definition uses the initializer. As a result, Clang rejects programs that GCC and MSVC accept, and it accepts the same code in a non-template class.
C++23 (ISO/IEC 14882:2024), quoted from its final draft N4950. [[class.mem.general]/11]:
> An immediate invocation ([expr.const]) that is a potentially-evaluated subexpression ([intro.execution]) of a default member initializer is neither evaluated nor checked for whether it is a constant _expression_ at the point where the subexpression appears.
[[dcl.init.general]/17]:
> An immediate invocation ([expr.const]) that is not evaluated where it appears ([dcl.fct.default], [class.mem.general]) is evaluated and checked for whether it is a constant _expression_ at the point where the enclosing initializer is used in a function call, a constructor definition, or an aggregate initialization.
[dcl.fct.default]/5 says the same for a default argument. The wording came from CWG2631, which was applied to C++23 as a defect report.
```cpp
// clang++ -std=c++23 -fsyntax-only -DCASE=<1..4> repro.cpp
#include <source_location>
void runtime_only() {}
// Never a constant _expression_.
struct never_constant {
consteval never_constant(int) { runtime_only(); }
};
int f(never_constant = 0) { return 0; }
// A constant _expression_ only when function_name() is non-empty.
struct needs_function_name {
consteval needs_function_name(std::source_location loc) {
if (*loc.function_name() == '\0') runtime_only();
}
};
int g(needs_function_name = std::source_location::current()) { return 0; }
#if CASE == 1
// Nothing uses the initializer of m, so nothing may evaluate it. Clang rejects.
template <class T>
struct S {
S() = default;
int m = f();
};
using U = decltype(S<int>());
#elif CASE == 2
// The constructor S(int) uses the initializer of m. Clang rejects.
template <class T>
struct S {
S(int) {}
int m = g();
};
S<int> s(1);
#elif CASE == 3
// Control: case 2 without a template. All compilers accept.
struct S {
S(int) {}
int m = g();
};
S s(1);
#elif CASE == 4
// Control: the constructor S(int) uses an initializer that is never constant. All compilers reject.
template <class T>
struct S {
S(int) {}
int m = f();
};
S<int> s(1);
#endif
```
Compiler Explorer, with Clang trunk, GCC trunk, and MSVC side by side:
| Case | What it shows | Link |
|---|---|---|
| 1 | The initializer is never used, and it is never a constant _expression_ | https://godbolt.org/z/asnzMaTEG |
| 2 | The constructor `S(int)` uses the initializer | https://godbolt.org/z/Mcnvr1j43 |
| 3 | Control: case 2 without a template | https://godbolt.org/z/xbYY8jxYj |
| 4 | Control: the constructor `S(int)` uses an initializer that is never a constant _expression_ | https://godbolt.org/z/1sqMe1bEz |
- Case 1 needs no particular value. `decltype` is an unevaluated operand ([expr.context]/1), so `S()` is not odr-used and not defined ([dcl.fct.def.default]/5). No constructor definition uses the initializer, so [dcl.init.general]/17 never requires the evaluation. Clang evaluates it while it computes the exception specification of `S()`. Whether the initializer is potentially-throwing ([except.spec]/7.3) needs no evaluation.
- In cases 2 and 3, the program is valid when `function_name()` is non-empty in the constructor. The value is implementation-defined ([support.srcloc.cons]). Clang's own value is non-empty, as the next section shows.
| Compiler (`-std=c++23 -fsyntax-only`) | Case 1 | Case 2 | Case 3 | Case 4 |
|---|---|---|---|---|
| Expected under C++23 | accept | accept | accept | reject |
| Clang trunk, 23.1.0, 22.1.0, 21.1.0 | reject | reject | accept | reject |
| GCC trunk | accept | accept | accept | reject |
| MSVC v19.latest (`/std:c++latest`) | accept | accept | accept | reject |
The expected result of cases 2 and 3 assumes a non-empty `function_name()` in the constructor, which Clang and GCC both give, as the run-time check below shows.
Clang trunk on Compiler Explorer, 2026-11-02, case 1:
```
<source>:10:24: error: call to consteval function 'never_constant::never_constant' is not a constant _expression_
<source>:24:5: note: in instantiation of default member initializer 'S<int>::m' requested here
<source>:27:20: note: in evaluation of exception specification for 'S<int>::S' needed here
<source>:25:13: note: in the default initializer of ''
<source>:10:7: note: declared here
<source>:8:37: note: non-constexpr function 'runtime_only' cannot be used in a constant _expression_
<source>:10:24: note: in call to 'never_constant(0)'
<source>:4:6: note: declared here
<source>:10:24: error: call to consteval function 'never_constant::never_constant' is not a constant _expression_
<source>:27:20: note: in evaluation of exception specification for 'S<int>::S' needed here
<source>:25:13: note: in the default initializer of ''
<source>:10:7: note: declared here
<source>:8:37: note: non-constexpr function 'runtime_only' cannot be used in a constant _expression_
<source>:10:24: note: in call to 'never_constant(0)'
<source>:4:6: note: declared here
2 errors generated.
```
Case 2:
```
<source>:18:29: error: call to consteval function 'needs_function_name::needs_function_name' is not a constant _expression_
<source>:33:5: note: in instantiation of default member initializer 'S<int>::m' requested here
<source>:36:8: note: in instantiation of member function 'S<int>::S' requested here
<source>:34:13: note: in the default initializer of ''
<source>:18:7: note: declared here
<source>:15:43: note: non-constexpr function 'runtime_only' cannot be used in a constant _expression_
<source>:18:29: note: in call to 'needs_function_name({&unnamed-global-constant})'
<source>:4:6: note: declared here
1 error generated.
```
## Which name Clang sees
This probe calls a different non-constexpr function for an empty and a non-empty `function_name()`, so each diagnostic shows what one evaluation saw. Compiler Explorer, Clang trunk: https://godbolt.org/z/KMGxx1s75
```cpp
// Which function_name() does Clang see in each evaluation of the default argument?
#include <source_location>
void saw_empty_name() {}
void saw_function_name() {}
struct probe {
consteval probe(std::source_location loc) {
if (*loc.function_name() == '\0') saw_empty_name(); else saw_function_name();
}
};
int g(probe = std::source_location::current()) { return 0; }
template <class T>
struct S {
S(int) {}
S(double) {}
int m = g();
};
S<int> a(1);
S<int> b(1.0);
```
```
<source>:12:15: error: call to consteval function 'probe::probe' is not a constant _expression_
<source>:16:5: note: in instantiation of default member initializer 'S<int>::m' requested here
<source>:20:8: note: in instantiation of member function 'S<int>::S' requested here
<source>:18:13: note: in the default initializer of ''
<source>:12:7: note: declared here
<source>:9:43: note: non-constexpr function 'saw_empty_name' cannot be used in a constant _expression_
<source>:12:15: note: in call to 'probe({&unnamed-global-constant})'
<source>:4:6: note: declared here
<source>:12:15: error: call to consteval function 'probe::probe' is not a constant _expression_
<source>:20:8: note: in instantiation of member function 'S<int>::S' requested here
<source>:18:13: note: in the default initializer of ''
<source>:12:7: note: declared here
<source>:9:66: note: non-constexpr function 'saw_function_name' cannot be used in a constant _expression_
<source>:12:15: note: in call to 'probe({&unnamed-global-constant})'
<source>:5:6: note: declared here
<source>:12:15: error: call to consteval function 'probe::probe' is not a constant _expression_
<source>:21:8: note: in instantiation of member function 'S<int>::S' requested here
<source>:18:13: note: in the default initializer of ''
<source>:12:7: note: declared here
<source>:9:66: note: non-constexpr function 'saw_function_name' cannot be used in a constant _expression_
<source>:12:15: note: in call to 'probe({&unnamed-global-constant})'
<source>:5:6: note: declared here
3 errors generated.
```
Clang evaluates the default argument three times. The first evaluation runs when Clang instantiates the default member initializer, and it sees an empty `function_name()`. The other two run in `S(int)` and `S(double)`, and see a non-empty name. Only the first evaluation is at a point that C++23 forbids, and only that one fails in case 2.
At run time, without `consteval`, the initializer sees the constructor's name on both compilers: https://godbolt.org/z/v5h8KqGY7
```
Clang trunk: S(int): 'S<int>::S(int) [T = int]' S() = default: 'S<int>::S() [T = int]'
GCC trunk: S(int): 'S<T>::S(int) [with T = int]' S() = default: 'constexpr S<T>::S() [with T = int]'
```
So Clang's own implementation-defined value at the point of use makes case 2 valid. The extra evaluation rejects valid code, and the run-time value is correct.
Possibly related: #104908 (same `in the default initializer of` note, different trigger).
AI assistance: Claude (Anthropic) reduced the test case, ran the compilers on Compiler Explorer, checked the cases against the C++23 final draft N4950, and drafted this report. I reviewed the test case, the results, and the text.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs