0

ではなく、どうすればcerr印刷できますか? Boost と Qt にアクセスできます。5 < 6statement_

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 に答える