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

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.

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.

Function returning a pointer to the same function [duplicate]

This question already has answers here:
Can a function return a pointer to its own type?
(2 answers)
Function Returning Itself
(10 answers)
Closed 3 months ago.
In this code a function returns a pointer to itself
typedef void (*voidfunc)();
voidfunc f(int) {
return (voidfunc) f;
}
and generates the assembly was expecting, but uses a cast to a different function type to do so. How can this be done without casting in C++?
In other words what can be placed instead of ??? in the following snippet?
??? f(int) {
return f;
}
I tried first with
auto f(int) {
return f;
}
but it doesn't work, then I tried with templates but it didn't work either.
It is impossible since the type of the function would need to be contain an infinite recursion.
The version with the cast is also not useful. Any actual use of the return value would need to cast back to the actual function type.
Without additional information about the use case it is difficult to make a recommendation to solve this.

How to Fix MISRA C++ Rule 0-1-4 [duplicate]

This question already has answers here:
C++11 range-based for loops without loop variable
(11 answers)
Closed 10 months ago.
The following code violates the MISRA C++ rule 0-1-4:
for (auto &a : b) {
... // The variable a is used only in the for condition.
}
Rule: A project shall not contain non-volatile POD variables having only one use. Variable 'a' is used only once, that is, during initialization.
What I tried:
for (const auto &a : b) {
... // The variable a is used only in the for condition.
}
But that was not the solution.
Does anyone have an idea how I can fix it?
Alas the current C++ grammar requires you to declare a variable when using the range-for form of the for loop:
for (auto& : b) {
is not allowed, despite it having potential applications (such as computing the number of elements in a container).
Writing
a;
or
(void)a;
in the loop body might work depending on the type. This would fool the static analyser and you can hope the expression gets compiled out.

What does " [this]" mean? [duplicate]

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.

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.