替换C/C++宏中的左括号字符

Cat*_*h33 3 c c++ macros

我需要一个宏来传递__FILE____LINE__传递一个具有默认参数的函数.这已经打开了一堆蠕虫,因为带有宏的默认args既不可能也不是非常混乱,如果可能的话我需要支持GCC和MSVC:

class Class
{
#ifdef _DEBUG

   int Function(int a, int b = 10, int c = 20) { return a + b + c; }

#else

   int DebugFunction(const char* filename, int lineNo, int a, int b = 10, int c = 20)
   {
      printf("%s (%i) a:%i b:%i c:%i\n", filename, lineNo, a, b, c);
      return a + b + c;
   }

   //Not possible
   #define Function( DebugFunction(__FILE__, __LINE__

#endif
}
Run Code Online (Sandbox Code Playgroud)

我试过\逃避(无济于事.代码库是巨大的,所以修复缺少的默认args或创建多个宏不是一个流行的选择.

有解决方案吗

Ker*_* SB 6

你可以制作一个可变的宏:

#define Function(...) DebugFunction(__FILE__, __LINE__, __VA_ARGS__)
Run Code Online (Sandbox Code Playgroud)

由于您不能"重载"宏,这可能是您最好的选择.