Mention C++11 lambdas and std::function<> in event handling docs
When using a C++11 compiler, it is very convenient to use lambdas directly as event handlers, so explicitly give an example of doing this in the documentation. Closes #17294.
This commit is contained in:
parent
a775d57f97
commit
4e00229b6e
@ -58,7 +58,8 @@ unbind, the handlers dynamically, i.e. during run-time depending on some
|
|||||||
conditions. It also allows the direct binding of events to:
|
conditions. It also allows the direct binding of events to:
|
||||||
@li A handler method in another object.
|
@li A handler method in another object.
|
||||||
@li An ordinary function like a static method or a global function.
|
@li An ordinary function like a static method or a global function.
|
||||||
@li An arbitrary functor like boost::function<>.
|
@li An arbitrary functor such as boost::function<> or, in C++11,
|
||||||
|
std::function<> or a lambda expression.
|
||||||
|
|
||||||
The static event tables can only handle events in the object where they are
|
The static event tables can only handle events in the object where they are
|
||||||
defined so using Bind<>() is more flexible than using the event tables. On the
|
defined so using Bind<>() is more flexible than using the event tables. On the
|
||||||
@ -254,7 +255,8 @@ Now let us describe the semantic differences:
|
|||||||
which allows to bind an event to:
|
which allows to bind an event to:
|
||||||
@li A method in another object.
|
@li A method in another object.
|
||||||
@li An ordinary function like a static method or a global function.
|
@li An ordinary function like a static method or a global function.
|
||||||
@li An arbitrary functor like boost::function<>.
|
@li An arbitrary functor such as boost::function<> or, in C++11,
|
||||||
|
std::function<> or a lambda expression.
|
||||||
|
|
||||||
This is impossible to do with the event tables because it is not
|
This is impossible to do with the event tables because it is not
|
||||||
possible to specify these handlers to dispatch the event to, so it
|
possible to specify these handlers to dispatch the event to, so it
|
||||||
@ -368,10 +370,30 @@ MyFrame::MyFrame()
|
|||||||
}
|
}
|
||||||
@endcode
|
@endcode
|
||||||
|
|
||||||
A common example of a functor is boost::function<>:
|
In C++11 a lambda expression can be used directly, without having to define a
|
||||||
|
separate functor class:
|
||||||
|
|
||||||
@code
|
@code
|
||||||
|
MyFrame::MyFrame()
|
||||||
|
{
|
||||||
|
Bind(wxEVT_COMMAND_MENU_SELECTED,
|
||||||
|
[](wxCommandEvent&) {
|
||||||
|
// Do something useful
|
||||||
|
},
|
||||||
|
wxID_EXIT);
|
||||||
|
}
|
||||||
|
@endcode
|
||||||
|
|
||||||
|
Another common example of a generic functor is boost::function<> or, since
|
||||||
|
C++11, std::function<>:
|
||||||
|
|
||||||
|
@code
|
||||||
|
#if __cplusplus >= 201103L || wxCHECK_VISUALC_VERSION(10)
|
||||||
|
using namespace std;
|
||||||
|
using namespace std::placeholders;
|
||||||
|
#else // Pre C++11 compiler
|
||||||
using namespace boost;
|
using namespace boost;
|
||||||
|
#endif
|
||||||
|
|
||||||
void MyHandler::OnExit( wxCommandEvent & )
|
void MyHandler::OnExit( wxCommandEvent & )
|
||||||
{
|
{
|
||||||
@ -389,7 +411,7 @@ MyFrame::MyFrame()
|
|||||||
@endcode
|
@endcode
|
||||||
|
|
||||||
|
|
||||||
With the aid of boost::bind<>() you can even use methods or functions which
|
With the aid of @c bind<>() you can even use methods or functions which
|
||||||
don't quite have the correct signature:
|
don't quite have the correct signature:
|
||||||
|
|
||||||
@code
|
@code
|
||||||
|
Loading…
Reference in New Issue
Block a user