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

时间:2014-07-25 17:33:40

标签: c++ c++11 assert variadic-templates variadic-macros

我正在尝试编写类似于标准dbgassert的宏assert。除assert之外,我还想dbgassert打印任意数量的附加参数(包含调试信息)。

我目前所拥有的内容如下所示,改编自this SO answer。但我的代码中存在可变参数模板或宏的问题。如果我至少使用一个附加参数(OK行),那么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))
}

代码的原始版本。

#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'
} 

5 个答案:

答案 0 :(得分:5)

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

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

我知道的其他修补程序是该语言的gcc扩展:有关详细信息,请参阅this gcc文档页面,但您可以通过在前面添加双##来修复宏__VA_ARGS__

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

PS:
#是预处理器的字符串化运算符:它将宏参数的值转换为字符串文字,即i。即而不是粘贴1>2它粘贴"1>2"

答案 1 :(得分:1)

将令牌粘贴运算符(##)放在__VA_ARGS__之前。如果__VA_ARGS__为空,则会在__VA_ARGS__之前删除逗号。

您的宏将是:

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

请注意,令牌粘贴是GNU CPP扩展,是正确提及的其他海报之一(请参阅https://gcc.gnu.org/onlinedocs/cpp/Variadic-Macros.html)。

MS编译器(在VS2010下测试)不需要标记粘贴,如果__VA_ARGS__为空,它只删除尾随逗号,请参阅:http://msdn.microsoft.com/en-us/library/ms177415(v=vs.110).aspx

答案 2 :(得分:1)

除了@cmaster和@ ds27680的解决方案之外,我还能找到解决尾随逗号问题的另一种方法。由于__VA_ARGS__导致额外的逗号,我可以将__VA_ARGS__打包到std::tuple或函数调用中,并使用元组/结果作为实际函数的参数< / em>的。现在空__VA_ARGS__不会成为问题,因为它被打包成一个有效值(即空元组或空函数的返回值)。我想这在代码中要长一些,但在不涉及##的情况下更容易移植。

以上两种情况分别显示在下面代码中的dbgassertdbgassert1宏中。

#include <cassert>
#include <iostream>
#include <tuple>
using namespace std;

template <typename ...Args>
string print_tuple(tuple<Args...> tp) {
  return ""; //print the tuple...
}

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

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

void realdbgassert1(string info,const char *msg, const char *file, int line) {
  cout << "Assertion failed! \nFile " << file << ", Line " << line << endl 
       << "  Expression: " << msg << endl
       << "  Info: " << info << endl;
  std::abort();
}

template <typename ...Args>
string print_info(Args ... args) {
  return "";  //print stuff
}

#define dbgassert1(EX,...) \
  (void)((EX) || (realdbgassert1 (print_info(__VA_ARGS__),#EX,__FILE__, __LINE__),0))


int main() {
  dbgassert(1>2,"right","yes"); //OK
  dbgassert(1>2,"right"); //OK
  dbgassert(1>2); //OK now
  dbgassert1(1>2); //OK too
}

答案 3 :(得分:0)

您假设assert是通过调用__assert来实现的。这可能是一个特定实现的工作原理,但一般不能依赖它。

相反,请按照the documentation:测试您的情况并在失败时将诊断信息发送到标准错误,然后调用std::abort

答案 4 :(得分:-1)

你必须编写函数__assert的内容 - 如果你指定它是extern,你应该将包含函数定义的文件附加到编译过程中。如果您不知道如何编写多文件程序,我实在无法帮助您。