M
M
Mercury132021-09-22 19:00:35
C++ / C#
Mercury13, 2021-09-22 19:00:35

Is it possible to make code that, if the constexpr expression is true, will issue a warning?

Task: display a warning in the log when debugging occurs: logging, imitation of rare code branches, and the like.
For what? - so that the programmer does not let the program into production in debug mode.

 inline void dbg() {}

/// [+] simulate changed horizon when actually it did not
constexpr bool SIMULATE_CHANGED_HORIZON = false;
inline bool chgNot(bool x) {
    if constexpr (SIMULATE_CHANGED_HORIZON) {
        dbg();
        return x;
    } else {
        return !x;
    }
}


Unfortunately, this code always warns - even if we have false and dbg () does not compile into it.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mercury13, 2021-09-22
@Mercury13

Invented.

 byte debugWarning = SIMULATE_CHANGED_HORIZON * 1000;

If false, compilation will run smoothly. If true, the 1000 → byte conversion will issue a warning.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question