| Issue |
176141
|
| Summary |
Clang accepts uninitialized local in constexpr function
|
| Labels |
clang
|
| Assignees |
|
| Reporter |
vrukesh
|
**What code / commands /steps will reproduce the problem?**
Compile the below sample code.
_template <class T>
constexpr T func(T x) {
T a;
a = x;
return a;
}
constexpr double v = func(1.5);
int main() {}_
**Compiler Explorer link:**
https://godbolt.org/z/K5v34WK9G
**What is the expected result?**
As per C++17 standard,
_The definition of a constexpr function shall satisfy the following requirements:
* it shall not be virtual;
* its return type shall be a literal type;
* each of its parameter types shall be a literal type;
* its function-body shall be = delete, = default, or a compound-statement that does not contain
* an asm-definition,
* a goto statement,
* an identifier label,
* a try-block, or
* a definition of a variable of non-literal type or of static or thread storage duration or for which no initialization is performed._
**What happens instead?**
Clang incorrectly compiles the code, without any diagnostic warning or error.
To meet the C++17 standard, Clang must throw diagnostic error.
GCC throws diagnostic compilation error.
_<source>:8:26: error: 'constexpr T func(T) [with T = double]' called in a constant _expression_
8 | constexpr double v = func(1.5);
| ~~~~^~~~~
<source>:2:13: note: 'constexpr T func(T) [with T = double]' is not usable as a 'constexpr' function because:
2 | constexpr T func(T x) {
| ^~~~
<source>:3:7: error: uninitialized variable 'a' in 'constexpr' context
3 | T a;
| ^
Compiler returned: 1_
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs