Der folgende Code von GCC 7.2 und 5.0.0 Klappern akzeptiert wird, sondern wird von Microsoft VS 2017 15.5.0 Preview 5 und Intel C++ Kompilierer 19 zurückgewiesen:Lambda und dessen Argument als konstanter Ausdruck
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);
^
Wenn ich ersetzen f(x)
mit f(decltype(x){})
, beschweren sich sowohl Microsoft als auch Intel nicht. Ich verstehe, dass x
ist kein konstanter Ausdruck, aber es wird nicht innerhalb f
verwendet. Das ist wahrscheinlich der Grund, warum GCC und Clan sich nicht beschweren.
Ich denke, dass Microsoft und Intel Compiler diesen Code korrekt ablehnen. Was denken Sie?
@RichardHodges, '-std = C++ 14'. – Evgeny
OK jetzt denke ich, das ist ein Fehler von gcc und clang, da C++ 14 nicht mit constexpr Lambda kommt ... dies ist seit C++ 17 –
@WF verfügbar, das Lambda ist absichtlich nicht "constexpr" (in echter Code ist es nicht). – Evgeny