This lambda doesn't have state (not a closure), so it's an ordinary function.
Saving it therefore is the same problem as saving any function. It's not possible in general, but as long as you're loading it back into the exact same process, it may be possible in practice, just by reinterpret_cast
-ing the function pointer to a char*
and reading a sufficient number of bytes. This will be highly non-portable, though, and may not work at all on some architectures or with some compilers.
Again: There is no standard-compliant way to treat code as data.
On the other hand, there are symbolic expression libraries that allow capture of an expression tree using ordinary code syntax, but then you're not dealing with a functor at all (there is no code, only data).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…