7

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).

leemes
  • 44,967
  • 21
  • 135
  • 183
  • 1
    probably type erasure. – yngccc Oct 21 '13 at 16:37
  • 3
    Lambda is really a struct that defines `operator()()` and `function` stores it like every other callable object. – Griwes Oct 21 '13 at 16:38
  • 1
    `std::function` deals with different-sized objects all the time (function pointers, bound pointers to members, functors, lambdas); as often happens with type erasure classes, in line of principle it can use dynamic allocation and polymorphism mixed with some template magic to do its job (although I wouldn't be surprised if it had some "small object optimization" in place). – Matteo Italia Oct 21 '13 at 16:38
  • Let me answer that with another question: how does `std::function` store the members of a function object? (Lambdas are not special) – R. Martinho Fernandes Oct 21 '13 at 16:40
  • OK, the lambda was an example, of course the same problem arises for any functor. – leemes Oct 21 '13 at 16:44
  • @aaronman: You should provide a link to the question, not a particular answer. Specially considering that the answer you link is not correct: *I doubt that `std::function` needs to allocate heap space*. Otherwise, good call on the duplicate question. – David Rodríguez - dribeas Oct 21 '13 at 16:46
  • @DavidRodríguez-dribeas that was an accident being a product of how I remembered I'd seen the answer somewhere, I'll fix it – aaronman Oct 21 '13 at 16:47
  • Yup, that question is pretty much the same. Voting my own question to close ;) – leemes Oct 21 '13 at 16:47
  • http://stackoverflow.com/questions/18453145/how-is-stdfunction-implemented, here is the link to the actual question – aaronman Oct 21 '13 at 16:47
  • @aaronman: You should correct your answer to that other question, there is no point in leaving a knowingly wrong answer stay there – David Rodríguez - dribeas Oct 21 '13 at 16:50

0 Answers0