3

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

下面列出了我到目前为止所拥有的内容,它改编自这个 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'
} 
4

5 回答 5

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))

Ps:是预处理器
#字符串化操作符:它把宏参数的值变成字符串字面量,即不是粘贴1>2而是粘贴"1>2"

于 2014-07-25T20:33:33.293 回答
1

就像一个注释一样,除了@cmaster 和@ds27680 的解决方案之外,我还能够找到另一种解决逗号问题的方法。由于 has__VA_ARGS__导致额外的逗号,我可以打包__VA_ARGS__到一个std::tuple或一个函数调用中,并将元组/结果用作实际函数的参数。现在 empty__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
}
于 2014-07-26T04:22:53.650 回答
1

将标记粘贴运算符 (##) 放在 __VA_ARGS__. 这将在__VA_ARGS__if__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

于 2014-07-25T21:01:17.067 回答
0

您假设这assert是通过调用实现的__assert。这很可能是一种特定实现的工作方式,但通常不能完全依赖。

相反,请遵循文档:测试您的状况并在失败时将诊断信息发送到标准错误,然后调用std::abort.

于 2014-07-25T17:55:32.027 回答
-1

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

于 2014-07-25T17:47:03.080 回答