I have a simple window application with declared main window callback procedure:
WNDCLASSEXW wcx;
/* ... */
wcx.lpfnWndProc = MainWndProc;
and after the WinMain
I declared LRESULT CALLBACK MainWndProc(HWND mainWindow, UINT msg, WPARAM wparam, LPARAM lparam) { /* ... */}
and all is working ok, but I wonder is it possible to have this MainWndProc
as a lambda inside WinMain ?
You can use a lambda, but it must not capture any variable in [ ], for example:
works in Visual C++ 2012.
You can use a lambda, provided it has no captures then it has an implicit conversion to function pointer:
The problem really is how much you can usefully do in a lambda as a callback without captures. You can still resort to "globals", in the same way you might with a regular function as the callback.
With a wrapping class, you can do it using the old technique of storing the "this" pointer as cargo data on the HWND.
One limitation of this technique is that you can't process any messages that arrive before WM_CREATE, which is the message that carries the creation parameter (there's only a handful of these early messages, and they are quite exotic).
Sample use of the utility above: