Clang accepts uninitialized local in constexpr function
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/issue-subscribers-clang-frontend
Author: Vrukesh Panse (vrukesh)
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
This was made valid by P1331, which doesn't appear to have been moved in DR status. Looks like the mistake was here: we still need the "uninitialized" diagnostic here for C++17 and earlier.
This was made valid by P1331, which doesn't appear to have been moved in DR status. Looks like the mistake was here: we still need the "uninitialized" diagnostic here for C++17 and earlier.
I was just looking at this, it has C++23 status: https://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#2558