Given a lambda function with a captured variable, like
[&x] (int y) { x += y; }
where is the context (here a reference to some variable int x
defined anywhere within the lexical context) stored when I pass this lambda to a variable of type std::function<void(int)>
?
I know dynamic size storages like std::vector
or std::string
, but they all store a fixed type of values (once the template parameters are known of course). But in the case of lambdas, such a storage should be able to store any type of values, known at compile time of the client code which assigns a lambda to a std::function
, but not known when passing the function around.
I mean, when we fix the concrete type of std::function
, we fix the function signature, but not the types of the captured variables / references. There got to be some dynamic size and dynamic type storage behind the scene. Clearly, this can't be for example a std::tuple
because that would require the types to be known when instancing the std::function
but they are not. So I wonder how it's implemented (e.g. in g++, but there might be a compiler-independent answer to this question).