Yesterday I was thinking about whether it would be possible to use the convenience of C++0x lambda functions to write callbacks for Windows API functions.
For example, what if I wanted to use a lambda as an EnumChildProc
with EnumChildWindows
? Something like:
EnumChildWindows(hTrayWnd, CALLBACK [](HWND hWnd, LPARAM lParam) {
// ...
return static_cast<BOOL>(TRUE); // continue enumerating
}, reinterpret_cast<LPARAM>(&myData));
Another use would be to write extern "C"
callbacks for C routines. E.g.:
my_class *pRes = static_cast<my_class*>(bsearch(&key, myClassObjectsArr, myClassObjectsArr_size, sizeof(my_class), extern "C" [](const void *pV1, const void *pV2) {
const my_class& o1 = *static_cast<const my_class*>(pV1);
const my_class& o2 = *static_cast<const my_class*>(pV2);
int res;
// ...
return res;
}));
Is this possible?
I can understand that lambdas that capture variables will never be compatible with C, but it at least seems possible to me that capture-nothing lambdas can be compatible.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…