c - How to make #if #endif part of a macro -
#define m(n)\ #if n == 5\ /*several lines of code can't replaced tertnary operator*/ #else\ n;\ #endif
when use macro so
m(5);
i expect output
// within #if #else block
but doesn't compile.
i'm not surprised doesn't compile: know #if cannot used on continuation line ie "\".
i've tried
#define pound_if #if
and use pound_if doesn't work.
is possible do?
is there nifty boost pre-processor stuff can used?
succinctly, can't. can rely on optimizer instead, though:
#define m(n)\ { if (n == 5) { \ /*several lines of code can't replaced ternary operator*/ \ } else { n; } } while (0)
if compiler can determine value of n
5 when code run (e.g. write m(5)
), code in body of if
included in generated code. if compiler can determine value of n
not 5 when code run, generate code in body of else
clause. , if can't determine value be, preprocessor not have been able either, compiler include code.
Comments
Post a Comment