Open
Description
The following program is rejected by clang but accepted by gcc and msvc. Demo
template <typename T>
constexpr auto func(T c) {
constexpr auto k = c();
return k;
};
int main() {
constexpr int i = 0;
constexpr auto lambda = []()constexpr { constexpr int j = i; return j; }; //compiles
constexpr auto lambda2 = [i]()constexpr { constexpr int j = i; return j; };//compiles
constexpr auto a = func(lambda); //clang and others compiles this
constexpr auto b = func(lambda2); //clang doesn't compile this
}
Clang says:
<source>:3:20: error: constexpr variable 'k' must be initialized by a constant expression
3 | constexpr auto k = c();
| ^ ~~~
<source>:18:24: note: in instantiation of function template specialization 'func<(lambda at <source>:13:30)>' requested here
18 | constexpr auto b = func(lambda2); //clang doesn't compile this
| ^
<source>:13:65: note: function parameter 'c' with unknown value cannot be used in a constant expression
13 | constexpr auto lambda2 = [i]()constexpr { constexpr int j = i; return j; };//compiles
| ^
<source>:3:24: note: in call to 'c.operator()()'
3 | constexpr auto k = c();
| ^~~
<source>:2:23: note: declared here
2 | constexpr auto func(T c) {
| ^
1 error generated.
Compiler returned: 1