The following code is adopted by GCC 7.2 and clang 5.0.0, but is rejected by Microsoft VS 2017 15.5.0 Preview 5 and the Intel C ++ 19 compiler:
struct S { }; constexpr int f(S) { return 0; } int main() { auto lambda = [](auto x) { constexpr int e = f(x); }; lambda(S{}); }
Microsoft:
<source>(12): error C2131: expression did not evaluate to a constant
Intel:
<source>(12): error: expression must have a constant value constexpr int e = f(x); ^ <source>(12): note: the value of parameter "x" (declared at line 10) cannot be used as a constant constexpr int e = f(x); ^
If we replace f(x) with f(decltype(x){}) , then both Microsoft and Intel will not complain. I understand that x not a constant expression, but not used inside f . This is probably why GCC and clang are not complaining.
I think the Microsoft and Intel compilers are correct in abandoning this code. What do you think?
c ++ language-lawyer lambda c ++ 14
Evgeny
source share