以下代码被GCC 7.2和clang 5.0.0接受,但被Microsoft VS 2017 15.5.0 Preview 5和Intel C++编译器19拒绝:
struct S { };
constexpr int f(S)
{
return 0;
}
int main()
{
auto lambda = [](auto x)
{
constexpr int e = f(x);
};
lambda(S{});
}
Run Code Online (Sandbox Code Playgroud)
微软:
<source>(12): error C2131: expression did not evaluate to a constant
Run Code Online (Sandbox Code Playgroud)
英特尔:
<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 …Run Code Online (Sandbox Code Playgroud)