According to Godbolt's Compiler Explorer (see demo), the following code compiles with GCC (both 10.2 and trunk) and outputs 3628800, but fails to compile with Clang (both 11.0.1 and trunk). In both cases, -std=c++17 is used. It also compiles with MSVC 19, but not with other compilers. Why is that and whose behavior is correct?
#include <iostream>
int main()
{
std::cout << [](auto f) { return f(f); }(
[](auto& h) {
return [&h](auto n) {
if (n < 2)
return 1;
else
return n * h(h)(n - 1);
};
})(10) << std::endl;
}
Further, even GCC and MSVC reject the code if I replace auto n with int n or if I replace if-else with the ternary operator (return n < 2 ? 1 : (n * h(h)(n - 1));).