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.
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:
Define variable b of the same type as variable a
(4 answers)
Closed 4 years ago.
I want gets the type of the auto variable and forces another variable become this type,i don't know whether c++ has such feature or function.
Use decltype.
auto a = 42;
decltype(a) b;
This question already has an answer here:
About meaning of ::* on this code [duplicate]
(1 answer)
Closed 5 years ago.
While refactoring a piece of code I came across the line below:
class Bar
{
protected:
int (Bar::* fooFunction)(float); //this line
}
I have never seen this kind of syntax before. What is this syntax and why is it used it used in C++?
It's a member function pointer.
Specifically, it's a pointer to a member function of a Bar object that takes a float argument and returns a int.
Read more here: http://en.cppreference.com/w/cpp/language/pointer#Pointers_to_member_functions
What is this syntax
It declares a pointer to member function.
why is it used it used in C++?
It is used to point to non-static member functions.
This question already has answers here:
Meaning of 'const' last in a function declaration of a class?
(12 answers)
Closed 8 years ago.
I saw something like:
const char& operator[] (int Index) const
the first const I understand. It's to protect the returning char from being modified.
But what does second const mean? Why do we sometimes use two const, sometimes just one?
It can be used for any member function, not only operators. It means, that this function will:
not be able to modify any data members (except mutable ones)
will not be able to call any non-const member functions