A lambda expression creates a temporary object (called a closure) of an unknown class type. Temporary objects can't be assigned to non-const references. That means you need
auto&& getx = [&]() {
return x;
}
so that you get an rvalue reference to the closure, or
auto getx = [&]() {
return x;
}
so you just get the closure.
That will get the code to compile, but still needs one more bit to make getx's return value to be a reference to x. To do that you need to add
auto getx = [&]() -> int& {
return x;
}
// or
auto getx = [&]() -> auto& {
return x;
}
// or
auto getx = [&]() -> decltype(auto) {
return x;
};
Also note that main must always return an int. You should turn up your compiler warning level so that it errors out if you try and use void main.