0

我怎样才能cerr打印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();
  }
}
4

2 回答 2

4

您需要使用 stringize 运算符:

throw runtime_error(# statement_);

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

于 2010-12-21T01:06:30.423 回答
1

哦,我找到了这个

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

#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();
  }
}
于 2010-12-21T01:06:23.397 回答