-1

I came across this code and I can not make sense of it.

RegisterCallback(MgrTsk::NAME,                                                                                                               
    [=](uint16_t cmd, uint16_t value, uint32_t size, void* pData) -> bool     {                                                              
        return MsgFromTsk(cmd, size, pData);                                                                                             
});

The return type of MsgFromTsk is bool. The API for RegisterCallback is -

template<typename F>                                                                                                                          
void RegisterCallback(const char* procName, F msgCallback) 

This probably might be a simple question, but even after a lot of google-ing I couldn't understand the syntax.

tinkerbeast
  • 1,402
  • 1
  • 13
  • 37

2 Answers2

1

In the call to RegisterCallback in the first block you posted, that function is called with MgrTsk::NAME as a first argument and

 [=](uint16_t cmd, uint16_t value, uint32_t size, void* pData) -> bool {                                                              
    return MsgFromTsk(cmd, size, pData);}

as a second argument.

Thas is, the template parameter F is now a lambda that takes uint16_t, uint16_t, uint32_t, void* as arguments and returns bool, and it calls MsgFromTsk to determine the returned value.

Toby Speight
  • 23,550
  • 47
  • 57
  • 84
Kai Guther
  • 743
  • 2
  • 11
1

This is a lambda:

[=](uint16_t cmd, uint16_t value, uint32_t size, void* pData) -> bool     {                                                              
    return MsgFromTsk(cmd, size, pData);                                                                                             
}
  • [...] is the capture-list of the lambda,
  • The =in [=] means that by default, the lambda captures variables by value,
  • (...) are the parameters passed to the lambda,
  • -> bool is the return type of the lambda,
  • {...}is the body of the lambda
Olivier Sohn
  • 1,262
  • 8
  • 18