繁体   English   中英

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

[英]How to write a c++ assert macro with a varying number of informational arguments?

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

到目前为止我所拥有的内容如下所示,它是根据这个SO答案改编的。 但我的代码中存在可变参数模板或宏的问题。 如果我至少使用一个附加参数(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'
} 

您的问题是__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))

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

__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( __VA_ARGS__)的.aspx

就像一个注释,除了@cmaster和@ ds27680的解决方案之外,我还能找到解决尾随逗号问题的另一种方法。 由于__VA_ARGS__导致额外的逗号,我可以将__VA_ARGS__std::tuple或函数调用中,并使用tuple / result作为实函数的参数。 现在空__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
}

你假设assert是通过调用实现__assert 这可能是一个特定实现的工作方式,但一般不能依赖。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM