My C++ is very rusty from non use so this may be a very trivial issue. I am running a DLL project in C++ on Visual Studio 2019. I have a define directive with parameter pack such as the following:
#define FUNC(...) \
try \
{ \
(__VA_ARGS__); \
} \
catch (std::exception & e) \
{ \
LOG("error: %s", e); \
return STATUSERROR; \
} \
return STATUSOK;
This works when i call:
FUNC( i = 0);
But when I try to call the following, it throws an error "expected an expression"
FUNC({
if(some_statement){
return something;
}
return something_else;
});
I am using \ as a line continuation escape character so the issue does not stem from that. I have compiled this with both C++ 14 and 17. What may I be missing here?
Your macro call expands to ({ /* stuff */ });
, which is not valid syntax. It may be that the code was originally compiled with GCC, which offers it as an extension.
You should remove the parentheses and semicolon from the macro expansion, leaving __VA_ARGS__
on its own to fill the enclosing scope.