首页 > 解决方案 > What does this oddly syntaxed C++ code with mean?

问题描述

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.

标签: c++

解决方案


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.


推荐阅读