C++ Logo


Advanced search

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

From: Thiago Macieira <thiago_at_[hidden]>
Date: Sun, 16 Apr 2023 09:20:24 -0300
On Friday, 14 April 2023 10:18:47 -03 Frederick Virchanza Gotham via Std-
Proposals wrote:
> int main(int argc, char **argv)
> {
> auto mylambda = [argc](void) -> void
> {
> std::cout << "Hello " << argc << "!" << std::endl;
> };
> Some_Library_Func( LambdaThunk(mylambda).getfuncptr() );
> cout << "Last line in Main" << endl;
> }

I know you didn't even test your code because it can't work, at least not
without some more code that you didn't include.

You created a LambdaThunk in the stack, then you JIT'ted code into that, then
would have attempted to jump to it. But that will only give you a crash,
despite having the right instructions, because the stack is not executable.
JIT is a reliable solution for all applications, because in hardened systems
it's completely prohibited: the system calls that would allow you to change a
page's attributes are denied.

Therefore, this is Dead On Arrival, at least as a general feature. Similarly,
as Zhihao Yuan wrote:

> And there is a paper trying to simplify that:
> void some_api(void cb(int) _Wide);
> https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2862.pdf

I hope this gets rejected for the same reason. It's trying to standardise bad
practice. Let's not do that. Instead, let's fix the real problem, which is bad
APIs: deprecate the existing ones and consign them to the historical dustbin
of "things not to do", then add new, better ones.
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
   Software Architect - Intel DCAI Cloud Engineering

Received on 2023-04-16 12:20:27