This question already has answers here:
Passing capturing lambda as function pointer
(10 answers)
Closed 6 days ago.
I want to call duk_push_c_function() with a lambda defined in C++, a bit like this:
SomeObjType parameter;
// store stuff in 'parameter'
auto myLambda = [parameter](duk_context* ctx) {
// do stuff with parameter
return (duk_ret_t)1;
}
duk_push_c_function(ctx, myLambda , 1);
My problem is that this won't compile because myLambda is not a C function:
error C2664: 'duk_idx_t duk_push_c_function(duk_context *,duk_c_function,duk_idx_t)': cannot convert argument 2 from 'MyObjectName::<lambda_07f401c134676d14b7ddd718ad05fbe6>' to 'duk_c_function'
Is there a nice way of passing a nameless function with parameters into duktape?
duk_push_c_function() expects a plain C-style function pointer. A non-capturing lambda can decay into such a pointer, but a capturing lambda cannot. So, you will have to store a pointer to your parameter in a stash where your lambda can reach it:
Stashes allow C code to store internal state which can be safely isolated from ECMAScript code.
Related
This question already has answers here:
What is a lambda expression in C++11?
(10 answers)
Closed 2 years ago.
ssc_event_cb_ts get_ssc_event_cb()
{
return
[this](const uint8_t *data, size_t size, uint64_t ts)
{
UNUSED_VAR(ts);
handle_ssc_event(data, size);
};
}
I can guess that the function get_ccs_event_cb is to return an anonymous function. If I want to learn more about this kind of declaration, what topic should I learn?
The get_ssc_event_cb() is returning a lambda, where [this] is the lambda's captures list. The lambda is capturing the this pointer of the object that get_ssc_event_cb() is being called on, so the lambda can call this->handle_ssc_event() when the lambda itself is called, eg:
someType obj;
auto cb = obj.get_ssc_event_cb();
cb(data, size, ts); // calls obj.handle_ssc_event(data, size);
You have a function returning a lambda. The expression inside [] in this context is known as lambda capture. It enables using specific variables from the surrounding scope inside the lambda.
Specifically, [this] means that get_ssc_event_cb() is a function member of a class, and the lambda can access members of the class. Apparently, handle_ssc_event() is such a member.
I found a website: Capture *this in lambda expression: Timeline of change. It's clear to learn.
This question already has answers here:
What is a lambda expression in C++11?
(10 answers)
Closed 4 years ago.
I am trying to understand what this declaration means. is it a function or a variable declaration? When I try to compile it in c or c++, it doesn't compile. However I found this code as part of a optimized solution to a question I was trying to solve, that is why I'm trying to figure it out.
int any = []() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
return 0;
}();
It is an immediately invoked lambda expression:
[] is an empty capture list;
() is an empty argument list;
{...} is a lambda body, that should return something that is convertible to an int, because it needs to be assigned to any.
Everything above defines a lambda.
() is a (function) call to that lambda with an empty argument list.
Lambda expressions are available since C++11, so maybe your compiler is using an outdated standard.
This question already has answers here:
Passing capturing lambda as function pointer
(10 answers)
C++ lambda with captures as a function pointer
(9 answers)
Closed 4 years ago.
I'm trying to create a list of function pointers, and these functions is passed as lambda.
string msg = "hello";
vector<string (*)() > myvector;
auto f = [=]() -> string {return msg; };
myvector.push_back(f);
cout << (*myvector[0])();
However, I got error in compiling when tried to capture variable and it successed when i didn't capture anything.
This problem occur when i use map, pair or sth similar.
funtionPointer.cc:36:22: error: no matching function for call to ‘std::vector<std::__cxx11::basic_string<char> (*)()>::push_back(main()::<lambda()>&)’
myvector.push_back(f);
Thank for any help.
C++ makes a distinction between types for function pointers and types for lambdas that have capture lists. The lambda you've made is callable as a function, but its type isn't "pointer to a function taking no arguments and returning void." More specifically, the type of that lambda expression will be some anonymous object type.
If you want to store a list of objects that can be called as a function that takes in no arguments and returns a string, consider making a vector of std::function<string()>, which can store a wider assortment of types callable as functions.
If you do this, to call those functions, you'd use the syntax
cout << myvector[0]() << endl;
rather than
cout << *(myvector[0])() << endl;
as you're no longer storing function pointers. (Fun fact - you don't actually have to dereference function pointers to call them!)
This question already has answers here:
Calling a function through its address in memory in c / c++
(6 answers)
Closed 5 years ago.
I have a function at a known memory address(for example: 0x11111111). The function returns an int, and takes a uint32_t pointer as its only argument.
How would I call this function using c++? I have seen a few examples of calling a function by its address, but I can't seem to find one that takes a pointer as its argument
EDIT:I seen that. Doesn't address how to call the function that takes a pointer as an argument
If you’re sure that there’s a function there, you could call it by casting the address to a function pointer of the appropriate type, then calling it. Here’s C code to do this:
typedef int (*FunctionType)(uint32_t*);
FunctionType function = (FunctionType)0x11111111;
function(arg);
This can easily be modified to support any number of function arguments and any return type you’d like. Just tweak the argument types list of the FunctionType typedef.
Or, in one line (gulp):
(((int (*)(uint32_t *)) 0x11111111)(arg);
This question already has answers here:
Should I use std::function or a function pointer in C++?
(6 answers)
Closed 8 years ago.
Is there any differences?
Which is the best way to "save/transfer" function?
function<void(int)> fcn =
[](int par) {std::cout<<"fcn: "<<par<<std::endl; };
void(*fcn_a)(int) =
[](int par) {std::cout<<"fcn_a: "<<par<<std::endl; };
fcn(12);
fcn_a(12);
std::function is more generic - you can store in it any callable object with correct signature (function pointer, method pointer, object with operator()) and you can construct std::function using std::bind.
Function pointer can only accept functions with correct signature but might be slightly faster and might generate slightly smaller code.
In the case of a non-capturing lambda, using a function pointer would be faster than using std::function. This is because std::function is a far more general beast and uses type-erasure to store the function object passed to it. It achieves this through type-erasure, implying that you end up with a call to operator() being virtual.
OTOH, non-capturing lambdas are implicitly convertible to the corresponding function pointer. If you need a full-fledged closure however, you will have to assign the lambda to std::function, or rely on type deduction through templates, whenever possible.