1

I order to exclude a block of code from the model during compiler time, I use preprocessors as:

#ifdef setting1
do something
#endif //setting1

Sometimes I intend to keep a piece of code during compilation but exclude it during run time. Is there a better way than an if statement for this purpose?

JNo
  • 89
  • 12
  • 3
    define "better" : is there a reason why `if` is *insufficient* for your needs? I mean, it's a clear-as-day mechanic for doing what you want, in both readability (literally self-documenting) and functionality, so is there some reason you *don't* want to use it ? – WhozCraig Jul 26 '19 at 09:13
  • "Better" meaning what, exactly? An `if` statement is as clear as it gets. – Federico klez Culloca Jul 26 '19 at 09:13
  • @WhozCraig maybe to distinguish it from a normal if – JNo Jul 26 '19 at 09:15
  • `switch (condition) { case true: { do something; break; } }` but that's just an ugly `if` statement. ;) – AlwaysLearning Jul 26 '19 at 09:15
  • @AlwaysLearning that's indeed an ugly if statement ;-) – JNo Jul 26 '19 at 09:15
  • 1
    @JalilNourisa and why wouldn't it be a normal `if`? What is a "normal `if`" anyway? – Federico klez Culloca Jul 26 '19 at 09:16
  • 2
    And why would you keep a piece of code which isn't executed at runtime depending on something defined at compile time? This sounds to me like an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) – Federico klez Culloca Jul 26 '19 at 09:17
  • 2
    Still struggling to understand the goal. Perhaps if your post included an example where you use `if`, and include an explanation of why you're looking for an alternative? run-time conditional execution is what `if` is literally intended for, so I'm curious where, and how, you consider it problematic. And I, too, am wondering what a "normal if" is if that isn't what we're talking about. – WhozCraig Jul 26 '19 at 09:20
  • @FedericoklezCulloca for debugging. That's why I wouldn't call it "normal if"! The reason I don't want to use a preprocessor is that the whole code is compiled every time I make a change to the settings file. So, If I can replace it with something like an "if", then I can put those settings into a txt file and get rid of the compilation headache. – JNo Jul 26 '19 at 09:30
  • That's still a "normal `if`". Use that and don't think too much about it. If it's clear that the flag is meant for debugging (because it's called something like `DEBUG_FLAG`) you won't have problems when re-reading the code. – Federico klez Culloca Jul 26 '19 at 09:33
  • @FedericoklezCulloca got it. thanks – JNo Jul 26 '19 at 09:35

1 Answers1

5

To keep it "raw" you can just replace the #ifdef with something like


bool const theflag = false;

if constexpr (theflag) {
    dosomething
}

This way the dosomething will still be syntax-checked.

bobah
  • 18,364
  • 2
  • 37
  • 70
  • 2
    And also const bool theFlag = false; – MofX Jul 26 '19 at 09:30
  • Can I do this kind of check for checking the platform? (i.e. Windows or Linux) – TonySalimi Jul 26 '19 at 09:45
  • 1
    @Hoodi - boost has a very comprehensive set of checks for all sorts of compiler brand/version and platform combinations, you could either use those or just cut/paste into your code, see https://stackoverflow.com/questions/46090534/how-to-get-platform-ids-from-boost – bobah Jul 26 '19 at 09:52
  • `theflag` should be `constexpr` too. – Piotr Siupa Jul 26 '19 at 13:16