0

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

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.