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

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

我正在尝试编写类似于标准 assert 的宏 dbgassert。除了 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'
} 

最佳答案

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

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

关于c++ - 如何编写具有不同数量的信息参数的 C++ 断言宏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24961400/

相关文章:

c++ - 如何避免意外地按值而不是按引用获取数组元素?

c++ - std::istream 类型是 EqualityComparable 吗?

python - 在普通代码中使用断言

java - java中的断言

c++ - 阻止写入 C++ Qt Qt5.0 中的控制台

c++ - 在没有 Microsoft 扩展的情况下在 Visual C++ 中构建

c++ - getline() 的奇怪行为

c++ - 在外部声明一个数组并使用初始化列表进行初始化

c++ - 双重检查锁定 : Fences and atomics

c# - 在 C# 中是否可以强制只能从构造函数调用私有(private)函数?