如何打印#define 语句?

发布于 2024-10-08 13:16:35 字数 586 浏览 0 评论 0原文

如何让 cerr 打印 5 5 5 5 5 5 < 6 而不是 statement_?我可以访问 Boost 和 Qt。

using namespace std;

#define some_func( statement_ )               \
  if( ! statement_ )                          \
  {                                           \
    throw runtime_error( "statement_" );      \
  }                                           \

int main()
{
  try
  {
    some_func( 5 < 6 );
  }
  catch(std::exception& e)
  {
    cerr << e.what();
  }
}

How can I get cerr to print 5 < 6 as opposed to statement_? I have access to Boost and Qt.

using namespace std;

#define some_func( statement_ )               \
  if( ! statement_ )                          \
  {                                           \
    throw runtime_error( "statement_" );      \
  }                                           \

int main()
{
  try
  {
    some_func( 5 < 6 );
  }
  catch(std::exception& e)
  {
    cerr << e.what();
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

逆光下的微笑 2024-10-15 13:16:35

您需要使用 stringize 运算符:

throw runtime_error(# statement_);

如果 statement_ 可能是宏,则您需要使用 双字符串化技巧

You need to use the stringize operator:

throw runtime_error(# statement_);

If statement_ may be a macro, you'll want to use the double stringize trick.

你的心境我的脸 2024-10-15 13:16:35

哦,我找到了这个

这是最终的工作代码=):

#include <stdexcept>
#include <iostream>

#define some_func( statement_ )              \
  if( ! statement_ )                         \
  {                                          \
    throw std::runtime_error( #statement_ ); \
/* Note: #, no quotes!       ^^^^^^^^^^  */  \
  }                                          \

int main(int argc, char** argv)
{
  try
  {
    some_func( 5 < 6 );
  }
  catch(std::exception& e)
  {
    std::cerr << e.what();
  }
}

Oh, I found this.

and here's the final working code =):

#include <stdexcept>
#include <iostream>

#define some_func( statement_ )              \
  if( ! statement_ )                         \
  {                                          \
    throw std::runtime_error( #statement_ ); \
/* Note: #, no quotes!       ^^^^^^^^^^  */  \
  }                                          \

int main(int argc, char** argv)
{
  try
  {
    some_func( 5 < 6 );
  }
  catch(std::exception& e)
  {
    std::cerr << e.what();
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文