Search code examples
c++macrosc-preprocessorstringification

How to print the #define statement?


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();
  }
}

Solution

  • 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.