can someone explain this short segment of C++ code, I can't make heads or tails of it

前端 未结 2 1626
南方客
南方客 2021-01-25 14:42
#include 
const char *getOpcodeName(
    uint8_t op
)
{
    #define OPCODE(x, y) if((0x##y)==op) return "OP_" #x;
        OPCODES
    #undef O         


        
2条回答
  •  忘掉有多难
    2021-01-25 15:40

    Run your code thru a C++ preprocessor, e.g. using g++ -Wall -C -E opcodes.cpp > opcodes.i then look inside the generated opcodes.i

    #define is not a statement but a preprocessor directive.

    The macro OPCODES gets expanded to some big chunk, notably containing OPCODE( NOP, 61) which would get expanded to something like

    if ((0x61)==op) return "OP_" "NOP";
    

    The two string literals are concatenated into one, "OP_NOP" here.

    GCC has a good documentation on its cpp preprocessor. Read about stringification (with the single # like the ending #x; of the OPCODE macro) and about concatenation (with a double ## like (0x##y) of the OPCODE macro).

提交回复
热议问题