I start with an example to elaborate my problem. And conclude with the exact statement of the question at the end.
So in C, we can write a macro like this,
#define NO_ERROR 0
#define RETURN_IF_ERROR(function) \
{ \
RetCode = function; \
if (RetCode != NO_ERROR) \
{ \
LOG(ERROR,"[%d] line [%d] [%s]", RetCode, __LINE__, __FILE__ ) ); \
return RetCode; \
} \
else { \
LOG(VERBOSE,"[%d] line [%d] [%s]", RetCode, __LINE__, __FILE__ ) );\
} \
}
Now this macro could be used in a function like this,
int ConstructAxes() {
RETURN_IF_ERROR(GetAxis("alpha"));
RETURN_IF_ERROR(GetAxis("beta"));
RETURN_IF_ERROR(GetAxis("gamma"));
RETURN_IF_ERROR(GetAxis("delta"));
.
.
.
}
So, we exit the current function (e.g. ConstructAxes) immediately with an error code, if one of the functions called within, returns with an error. Without that macro, for each of those 4 statements, I would have to write an if...else block. I'd end up with 4 lines of code that shows the actual functionality or task and then another 16 lines of error-checking (and/or optional logging). (I have seen functions which are 700+ lines long with only 20~30 lines of functionality and 600+ lines of if...else error checking and logging. Not very easy to follow the main logic then.)
(p.s. before anyone points out, I cannot use exceptions. This is a legacy project and exceptions are not used and not desired to be used, nor am I an expert at writing exception-safe code. Also before anyone points out, the returned error code is reinterpreted into some meaningful text, at a higher level. Where and how is not relevant to this question at hand.)
The question is, macros can be problematic and I'd prefer a function. So is there some clean&elegant way to do this in C++ without using a macro? I have a feeling it is NOT possible.