With this tutorial we'll give you a better view of how the lambda function can and will be constantly use in the C++ bindings.
For a more broad aproach you should do a little web research.
The syntax adopted for these examples:
[capture]
(parameters)
{body}
capture: Determinate how and if the capture occurs. Possible indicators, two or more should be intercalated by commas:
this
pointer by copyparameters: List of parameters necessary for each specific lambda function.
body: Function body
Let's start with a more simple lambda and later a more complex one, all extracted from elementary examples:
First Example - Button - Hello, Button! :
In this example we use a lambda function for elm::button btn that will be called when that button is clicked in callback_clicked_add( on_click ). This lambda will then ask to exit Elementary's main loop with elm_exit(). If this call is issued, it will flag the main loop to cease processing and return back to its parent function, usually your elm_main() function.
Now let's analize the sintax used for this lambda:
With [] we are signaling that we don't want to capture any variables and with () we are indicating that this lambda doesn't need parameters to work as it should. Now the important part of this function it's the body represented by {} where we are applying elm_exit() everytime this lambda is called.
In this case we are using std::bind to bind the parameters of our lambda function to return as std::function object to on_click which was declare as auto.
For this example with std::bind we simplified our work simply because we didn't have to search in the code or documentation of Elementary to look for the parameters and/or values that the callback_clicked_add requires of the function we are adding.
Second Example - Hoversel example with C++ Binding :
In this example we use a lambda function for hoversel that will be called when that hoversel is clicked in callback_clicked_add( add_item ). This lambda will then add an item to heversel, note that since we allocate memory for the item we need to know when the item dies so we can free that memory.
Now let's analize the sintax used for this lambda:
obj
to work as it should. Bbecause we are only adding the parameter we need instead of all the parameters this callback requires we need to use placeholders in std::bind, indicating the place that should occupy in our callback_clicked_add.When the function object returned by bind is called, an argument with placeholder _1 is replaced by the first argument in the call, _2 is replaced by the second argument in the call, and so on.
In this case we are using std::bind to bind the parameters of our lambda function to return as std::function object to add_item which was declare as auto.