Given a macro FOO
used like this:
std::string f1();
std::string f2();
FOO(f1().c_str(), f2().c_str());
Note: The type, std::string
is only an example. FOO
is generic and may not assume anything about types.
that is supposed to guarantee the order of evaluation of f1()
and f2()
by doing something like:
#define FOO(e1, e2) \
do { \
auto v1 = e1; \
auto v2 = e2; \
foo(e1, e2); \
} while(0)
Edit: unfortunately foo
can also be a template.
Unfortunately, that way the temporary returned by f1
is deleted and the c_str
becomes invalid for the call to foo
.
Is there a way to guarantee the order of expression evaluation for macro parameters while retaining all temporary lifetimes?
Surely there are overall better ways to approach that, but I'm specifically curious if there is a way to do this without reasoning about each of the usages of that macro in a large code base. Further I want to avoid handling specific types (i.e. not preserving const char*
with strdup
).