Consider the following piece of code
#include <iostream>
#include <functional>
namespace A {
template<typename T>
struct X {
using Function = std::function<int(T)>;
static Function f;
};
template<typename T>
typename X<T>::Function X<T>::f = [](auto) { return 42; };
}
int main() {
std::cout << A::X<int>::f(0);
}
Both GCC and clang accept this code but MSVC (tested version 19.00.23506) gives:
error C2888: 'auto <lambda_ce48e25aa4b9e3d225584044e4eae9e2>::operator ()(_T1) const': symbol cannot be defined within namespace 'A'
And indeed, if I remove namespace A and define everything in the global namespace, the code is accepted. Same if I make lambda expression non-generic.
Can someone explain what is the problem that MSVC sees in this code? Does C++ Standard restrict usage of generic lambdas in contexts like above?