title: How does std::function store the context of a lambda?
body: <p>Given a lambda function with a captured variable, like</p>
  
  <pre><code>[&amp;x] (int y) { x += y; }
  </code></pre>
  
  <p>where is the context (here a reference to some variable <code>int x</code> defined anywhere within the lexical context) stored when I pass this lambda to a variable of type <code>std::function&lt;void(int)&gt;</code>?</p>
  
  <p>I know dynamic size storages like <code>std::vector</code> or <code>std::string</code>, 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 <em>any</em> type of values, known at compile time of the client code which assigns a lambda to a <code>std::function</code>, but not known when passing the function around.</p>
  
  <p>I mean, when we fix the concrete type of <code>std::function</code>, we fix the <em>function signature</em>, but not the types of the captured variables / references. There got to be some dynamic size <em>and</em> dynamic type storage behind the scene. Clearly, this can't be for example a <code>std::tuple</code> because that would require the types to be known when instancing the <code>std::function</code> 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).</p>