lundi 13 janvier 2020

Getting an if-statement to also check for a defined macro

I have an if-else where the if should check a flag and whether a macro is defined. I've come up with two solutions but am unsure on which of them to use.

  • Will there be a difference in execution time (or will compiler optimization eliminate this)?
  • Can the first solution cause bugs with the else when the flag isn't defined (i.e. due to lines written above or below the example code)?

Additionally:

  • Are there any commonly followed best practices that could apply to this situation?

1

#ifdef FLAG_A
    if(flagB)
    {
        ...
    }
    else
#endif
    {
        ...
    }

2

#ifdef FLAG_A
    bool flagA = true;
#else
    bool flagA = false;
#endif

    if(flagA && flagB)
    {
        ...
    }
    else
    {
        ...
    }

Aucun commentaire:

Enregistrer un commentaire