How to print the #define statement?
-
12-10-2019 - |
Question
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.
OTHER TIPS
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();
}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow