Issue |
116969
|
Summary |
[clang] error: constexpr variable must be initialized by a constant _expression_
|
Labels |
clang
|
Assignees |
|
Reporter |
itou-fj
|
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
```
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs