c++ - Calling a function name built with __LINE__ -
suppose, have built unique function body below code:
#define tokenpaste(x, y) x ## y #define tokenpaste2(x, y) tokenpaste(x, y) #define unique static void tokenpaste2(unique_, __line__)(void) how can call function ?
macro definition taken from: creating c macro ## , __line__ (token concatenation positioning macro).
no. cannot. because cannot determine function name @ runtime. (i.e. either call unique_22 or unique_44. can call unique<22> or unique<44>)
so can use template solution instead. declare unique below:
template<unsigned int line> void unique (); and #define macro this:
#define unique template<> unique<__line__>() {} i advice use __counter__ instead of __line__ if compiler supports it. [note: means in line can call unique once , macro should expanded in global or namespace scope (not inside method).]
Comments
Post a Comment