C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Function Pointer from Lambda with Captures

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Mon, 17 Apr 2023 10:01:34 +0100
On Sun, Apr 16, 2023,Edward Catmur wrote:
>
> There's another technique that works as long as you're OK with having a limit
> to the number of simultaneous thunks outstanding. Basically, instead of a single
> thread-local data pointer you have a fixed-sized array of them, and a same-sized
> array of function pointers to hand out to the callback-based API.
> Demo: https://godbolt.org/z/cGra4Kjxn


I had been working with similar code posted by Alf P. Steinbach over
on comp.lang.c++ last week, but I much prefer your code Edward.

I have tweaked your code a bit so that the following syntax is possible:

    SomeLibraryFunc( thunk(f) );

Note that you don't have to explicitly tell it the lambda's function
signature, it figures it out itself.

I've also done a handful of other things:
    * Replaced the pair of pointers with just one pointer
    * Got rid of the 'used' array and instead just check for nullptr's
in the 'data' array
    * Mark all members of 'thunk' as 'noexcept'
    * Propagate the 'noexcept' from the lambda to the thunk
    * Accommodate lambas marked as 'mutable'

Here's what I've got:

    https://godbolt.org/z/5xT7e84WY

Received on 2023-04-17 09:01:47