Why and how does it work?
This works because:
- the lambda expression causes the creation of a unique class and name at compile time.
- The type of this class is called the internal compiler.
- Therefore, the compiler may find something like
<lambda_30560bd1c97ca682d011cd006c362574>::()::Local for x .
You can get the lambda type and then use it to declare objects of the enum class type contained in it:
auto lambda = []() { enum class Local { X = 0, Z = 1 }; return Local::X; }; int main() { auto x = lambda();
enum class Local not known outside of the lambda type a.
True, but an enum class is available if the corresponding namespace is allowed. Therefore, inside <lambda_30560bd1c97ca682d011cd006c362574>::()::Local you can enable Local inside lambda, but before compilation it is impossible to guess this name, but we can use decltype or auto to get the type.
This is a enum class and therefore cannot be of type int without cast
Correctly. But it still exists just as an enum class can exist inside a regular class or struct .
Without a known type, how can I continue to work with this value outside of lambda?
A utility of this type outside of lambda is limited. It is not int , but rather has its own unique type, so it does not really matter, even if it can be indirectly obtained.
wally
source share