Let's get the semantic around closure fixed.

deadalnix deadalnix at gmail.com
Wed May 19 23:37:58 UTC 2021


On Wednesday, 19 May 2021 at 21:56:19 UTC, Ola Fosheim Grostad 
wrote:
> On Wednesday, 19 May 2021 at 20:19:22 UTC, deadalnix wrote:
>> In fact, even in C++, you'll find yourself with an allocation 
>> per loop if you do:
>>
>> std::vector<std::function<void()>> funs;
>> for (int i = 0; i < 10; i++) {
>>     funs.push_back([i]() { printf("%d\n", i); });
>> }
>>
>> The instantiation of std::function here will allocate.
>
> I think it is implementation defined how large the internal 
> buffer in std::function is? So it will allocate if it is too 
> large? But yeah, it is ugly, I never use it. Usually one can 
> avoid it..

It is as large as it needs to be because you can capture 
arbitrarily large objects.

If it is small enough, some implementations of std::function can 
do small object optimization and do it in place. It is only 
guaranteed for raw function pointers.


More information about the Digitalmars-d mailing list