<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/116969>116969</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[clang] error: constexpr variable must be initialized by a constant expression
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
itou-fj
</td>
</tr>
</table>
<pre>
When I compiled this code with clang, it returned a compilation error.
Other compilers (gcc, msvc, etc.) can compile without errors.
[Compiler Explorer](https://godbolt.org/z/ErhdPo7Me)
Code:
```
class Apple {
public:
constexpr Apple() {}
constexpr operator int() { return 555; }
};
class Meal {
public:
//constexpr Meal(Apple apple) {} // OK
constexpr Meal(int apple) {} // NG
};
class Dinner : Meal {
public:
Apple apple_;
constexpr Dinner() : apple_(), Meal(apple_) {}
};
int main() {
constexpr Dinner my_dinner;
return 0;
}
```
Error Message:
```
<source>:18:39: warning: field 'apple_' is uninitialized when used here [-Wuninitialized]
18 | constexpr Dinner() : apple_(), Meal(apple_) {}
| ^
<source>:22:20: error: constexpr variable 'my_dinner' must be initialized by a constant expression
22 | constexpr Dinner my_dinner;
| ^~~~~~~~~
<source>:18:39: note: member call on object outside its lifetime is not allowed in a constant expression
18 | constexpr Dinner() : apple_(), Meal(apple_) {}
| ^~~~~~
<source>:22:20: note: in call to 'Dinner()'
22 | constexpr Dinner my_dinner;
| ^~~~~~~~~
1 warning and 1 error generated.
Compiler returned: 1
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysVU2PozgQ_TXOpdQRlEMTDhzyuVqtemdvcxwZXAkeGTuyTff0HPa3rxwgoTeZbq22UQTEqo_36lVRwnt1NEQly9Ys285EFxrrShVs93D4PqusfC2_NmTgd6hte1KaJIRGeaitJHhRoYFaC3NkuAEVwFHonCEJYjAXQVkD5Jx1c5ZsWbL6EhpyYzDngeHyWNfRv_XP5yeFes6wgFqY0e6cy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