如何编写带有不同数量的信息参数的c ++断言宏?

tin*_*lyx 3 c++ assert variadic-templates variadic-macros c++11

我正在尝试编写dbgassert类似于标准的宏assert。除了做什么以外assert,我还想dbgassert打印任意数量的其他参数(包含调试信息)。

下面列出了我到目前为止所拥有的,这是从SO答案改编而来的。但是我在代码中存在可变参数模板或宏的问题。如果我至少使用一个附加参数(“确定”行),则dbgassert可以按预期工作。但是,如果我不给出其他参数,则编译将失败(问题行)。

我在可变参数模板编程方面有一些经验(例如如何打印元组),但是我以前从未使用过可变参数宏。

能否请您解释编写这种可变参数宏组合的正确方法是什么?

顺便说一句,有人可以解释 #EX在宏中魔术吗?它显示了表达式并在gcc4.8.1上为我工作。它得到普遍支持吗?

谢谢,


码:

//corrected reserved identifier issue and assumption issues per comments
#include <cassert>
#include <iostream>
using namespace std;

template <typename ...Args>
void realdbgassert(const char *msg, const char *file, int line, Args ... args) {
  cout << "Assertion failed! \nFile " << file << ", Line " << line << endl 
       << "  Expression: " << msg << endl;
  std::abort();
}

#define dbgassert(EX,...) \
  (void)((EX) || (realdbgassert (#EX, __FILE__, __LINE__, __VA_ARGS__),0))

int main() {
  dbgassert(1>2,"right","yes"); //OK
  dbgassert(1>2,"right"); //OK.
  //dbgassert(1>2); //Problem. compile error: expected primary-expression before ')' token
                  //#define dbgassert(EX,...) (void)((EX) || (realdbgassert (#EX, __FILE__, __LINE__, __VA_ARGS__)^,0))
}
Run Code Online (Sandbox Code Playgroud)

代码的原始版本。

#include <cassert>
#include <sstream>
using namespace std;

#ifdef __cplusplus
extern "C" {
#endif
extern void __assert (const char *msg, const char *file, int line);
#ifdef __cplusplus
};
#endif

template <typename ...Args>
void _realdbgassert(const char *msg, const char *file, int line, Args ... args) {
    stringstream os;
    //... do something
    __assert(msg,file,line);
}
#define dbgassert(EX,...) (void)((EX) || (_realdbgassert (#EX, __FILE__, __LINE__, __VA_ARGS__),0))

int main() {
  dbgassert(1==0,"right"); //Problem line: undefined reference to `__assert'
} 
Run Code Online (Sandbox Code Playgroud)

cma*_*ter 5

您的问题是__VA_ARGS__在问题情况下其值为空。因此,当预处理器扩展时realdbgassert(#EX, __FILE__, __LINE__, __VA_ARGS__),结果是未完成的参数列表realdbgassert("1>2", "foo.c", 42, )。请注意,由于的空扩展,未正确终止参数列表__VA_ARGS__

要解决此问题,您需要使用某种技巧。最好的解决方案是调整环境,使其__VA_ARGS__包括最后一个无条件参数,并将其与可选参数一起传递给函数调用的末尾。最好,因为它是标准C。

其他的修复,我知道的,是gcc的一个扩展的语言:请参见更详细的信息gcc的文档页面,但是您可以通过添加双解决您的宏##在眼前__VA_ARGS__

#define dbgassert(EX,...) \
  (void)((EX) || (realdbgassert (#EX, __FILE__, __LINE__, ## __VA_ARGS__),0))
Run Code Online (Sandbox Code Playgroud)

PS:
#是预处理器的字串操作者:它把宏参数的值成一个字符串,即,代替粘贴的1>2它粘贴"1>2"