Deutsch English Français Italiano |
<vr1a2g$19unh$1@dont-email.me> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail From: David Brown <david.brown@hesbynett.no> Newsgroups: comp.lang.c Subject: Re: Concatenated if and preprocessor Date: Fri, 14 Mar 2025 14:13:52 +0100 Organization: A noiseless patient Spider Lines: 105 Message-ID: <vr1a2g$19unh$1@dont-email.me> References: <vquuhg$34o8d$2@dont-email.me> <vr15ti$rtjs$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Fri, 14 Mar 2025 14:13:53 +0100 (CET) Injection-Info: dont-email.me; posting-host="377a414a67785d33e2adba504c7765b6"; logging-data="1374961"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+EF5PAy/A4SYKFhkZlmg6HDfRzsiYe3DA=" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.11.0 Cancel-Lock: sha1:8WonvQmhwzV+QoP7wSFP63L6CMs= Content-Language: en-GB In-Reply-To: <vr15ti$rtjs$1@dont-email.me> Bytes: 3935 On 14/03/2025 13:02, pozz wrote: > Il 13/03/2025 16:44, pozz ha scritto: >> 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? > > You're right, a real example is better to define my problem. > > I have a project that can be compiled in different ways, depending on > the final product model (it's an embedded platform, it's a material > device). > I have three models with different characteristics: MODEL_A, MODEL_B, > MODEL_C. Three different builds that are managed at compile time by > defining MODEL macro as MODEL_A, MODEL_B or MODEL_C. > > For each model, I could have a different features set. FEATURE_1, > FUTURE_2, FUTURE_3, ... > > #if MODEL == MODEL_A > # define FEATURE_1 1 > # define FEATURE_2 0 > # define FEATURE_3 0 > #elif MODEL == MODEL_B > # define FEATURE_1 0 > # define FEATURE_2 1 > # define FEATURE_3 0 > #elif MODEL == MODEL_C > # define FEATURE_1 1 > # define FEATURE_2 1 > # define FEATURE_3 1 > #endif > > Now, on the full-featured model (MODEL_C) I could write: > > if (key == 1) { > ... > } else if (key == 2) { > ... > } else if (key == 3) { > ... > } else { > ... > } > > However, for MODEL_A I should have: > > if (key == 1) { > ... > } else { > ... > } > > For MODEL_B I should have: > > if (key == 2) { > ... > } else { > ... > } > > This is the scenario. > > I thought using if(0) or if(1), but many times I receive some warnings > from the compiler (condition is always true or condition is always false). > How about : if (FEATURE_1 && key == 1) { ... } else if (FEATURE_2 && key == 2) { ... } else { ... } ? (You might prefer a switch to a list of if's, but that's a different issue.) If you are still suffering from unwanted warnings (and don't want to disable the warning in question), can you post a godbolt.org link that shows some sample code giving the warning, along with the choice of compiler and flags you are using? When people can duplicate the issue, it's a lot easier to give help than when your code is approximate, your compiler messages are vague, and we don't know details of the toolchain. (Usually it does not matter if the toolchain is for the right target processor - you will generally get the same warnings for code for x86-64 gcc, and godbolt has most versions of these available.)