c++ - Why is the double pound needed in one place but not the others in this macro? -
in particular bit of code, author clever using macro create incrementor functions members of class counter.
class counter { public: uint64 _call; uint64 _call_indirect; uint64 _return; uint64 _syscall; uint64 _branch; uint64 _branch_indirect; counter() : _call(0),_call_indirect(0), _return(0), _branch(0), _branch_indirect(0) {} uint64 total() { return _call + _call_indirect + _return + _syscall + _branch + _branch_indirect; } }; counter countseen; counter counttaken; #define inc(what) void inc ## (int32 taken) { countseen. ++; if( taken) counttaken. ++;} inc(_call) inc(_call_indirect) inc(_branch) inc(_branch_indirect) inc(_syscall) inc(_return)
i'm little confused macro, though. why author use double pound in function definition part of macro , not when being used class variable being incremented?
edit: double pound concatenation, confusion comes why double pound not necessary in "countseen. ++" , "counttaken. ++" parts.
let’s @ first macro (inc(_call)
) call expands to:
void inc_call (int32 taken) { countseen. _call ++; if( taken) counttaken. _call ++;}
reformatting bit, get:
void inc_call (int32 taken) { countseen._call++; if(taken) counttaken._call++; }
this declares function, inc_call
, increments countseen._call
, maybe counttaken._call
. since variables aren’t called ._call
(which invalid token, since can’t start name period), period should not glued macro argument. similarly, since _call++
isn’t valid token (it’s 2 tokens; can’t include plus signs in name), there shouldn’t pound signs there.
however, without first ##
, result start void inc _call (int32 taken)
, invalid c++ because have 2 function names.
Comments
Post a Comment