Sujet : Re: Concatenated if and preprocessor
De : tr.17687 (at) *nospam* z991.linuxsc.com (Tim Rentsch)
Groupes : comp.lang.cDate : 13. Mar 2025, 22:37:16
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <868qp8txwz.fsf@linuxsc.com>
References : 1
User-Agent : Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
pozz <
pozzugno@gmail.com> writes:
Consider this code:
>
if (cond1) {
...
} else if (cond2) {
...
} else if (cond3) {
...
}
>
I want to activate every single if with a macro preprocessor. All the
combinations are possible: only the first, only the second, only the
third, the first and second... and so on.
>
What's the best method to have a clean code that is always compiled
without errors?
if ( ..bypass all further tests.. ) {
// for a "skip all conditional segments" case (if needed)
...
#if TEST_1
} else if (cond1) {
...
#endif
#if TEST_2
} else if (cond2) {
...
#endif
#if TEST_3
} else if (cond3) {
...
#endif
} else {
// for a "no conditional segment" ran case (if needed)
...
}
Having said that, it's hard to imagine a scenario where doing
something like this is the best way to solve the higher level
problem. It is almost certainly better to rethink the reasoning
that resulted in choosing this scheme, and find a way to avoid it.