Quite common that we have to call set of functions that have similar behavior - for example return negative value and set errno variable in case of failure. Instead of repeatedly write check code, sometimes it is useful to wrap it into macro:
#define CHECKED_CALL( func, ... ) \
do {\
auto ret = func( __VA_ARGS__ );\
if( ret < 0 ) {\
std::cerr << #func "() failed with ret " << ret << " errno " << errno << std::endl;\
throw std::runtime_error( #func "()" failed" );\
}\
while(false)
then use is simple:
CHECKED_CALL( func1, 1, 2, 3 );
CHECKED_CALL( func2, "foobar" );
etc. Now let's say I need to get result of the call in case it did not fail. Something like:
auto r = CHECKED_CALL( func3, 1.5 );
Obviously this would not work as written and of course I can write another macro, but question is, is it possible to write macro that would work in both cases? Or maybe not macro but not very complicated code, that will take me 10 times more time to implement than to write yet another macro for value return. Especially not to generate warning of unused code etc.