What does " [this]" mean? [duplicate] - c++

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.

Related

lambdas with duktape (using C++) [duplicate]

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.

Returning value from lambda in the same line with declaration

Sometimes it is easier to represent a value by function, and lambdas are good for this. But is there any way to return value from lambda declaration?
for example:
int i = []{return 2;};
generates an error. How to make that lambda declaration return 2 without any new lines of code?
Like calling any functions using the calling operator(), you need to call the lambda.
int i = []{return 2;}();
// ^^
Addtionaly to the answers provided. This is called an immediately-invoked function expression (IIFE), or sometimes, immediately-invoked lambda expression. (FF comes from C++ is widely used in other languages)
int i = []{return 2;}(); // () invokes the lambda AKA call operator

trying to understand what "int any = []() { //code body } ();" means [duplicate]

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.

Why isn't a lambda that captures variables by reference convertible to a function pointer?

If I have a lambda which captures all automatic variables by reference ([&] {}), why can't it be converted to a function pointer? A regular function can modify variables just like a lambda that captures everything by reference can, so why is it not the same?
I guess in other words, what is the functional difference between a lambda with a & capture list and a regular function such that the lambda is not convertible to a function pointer?
So let's take the example of a trivial lambda:
Object o;
auto foo = [&]{ return o; };
What does the type of foo look like? It might look something like this:
struct __unique_unspecified_blah
{
operator()() const {
return o;
}
Object& o;
};
Can you create a function pointer to that operator()? No, you can't. That function needs some extra information that comes from its object. This is the same reason that you can't convert a typical class method to a raw function pointer (without the extra first argument where this goes). Supposing you did create some this pointer - how would it know where to get o from?
The "reference" part of the question is not relevant - if your lambda captures anything, then its operator() will need to reference some sort of storage in the object. If it needs storage, it can't convert to a raw function pointer.
I guess in other words, what is the functional difference between a
lambda with a & capture list and a regular function such that the
lambda is not convertible to a function pointer?
References, though they aren't objects, need to be stored somewhere. A regular function cannot access local variables of another function; Only references (e.g. as parameters) that could refer to local variables. A Lambda with a & as the capture-default can, because every variable needed can be captured. In other words: A regular function doesn't have state. A closure object with captured variables does have state. So a closure object cannot be reduced to a regular function, because the state would be lost.

What does [](){} construction mean in c++? [duplicate]

This question already has answers here:
What is a lambda expression in C++11?
(10 answers)
Closed 9 years ago.
I have this code.
boost::for_each(
boost::make_iterator_range(
func(arg1),
func(arg2)
),
[&d, &f](const a<b>& c)
{
something;
}
);
I understand the iterator part of the code. What is not clear to me is over what we iterate. What does this construction mean? [](){}
This is a lambda-expression , an anonymous method/function. You can provide it inline if there is no reason to define a distinct function.
[] binds the local parameters either by value [] or by reference [&]. In () you pass your values like in a function call and {} embraces the function body.
See here.