The compiler may or may not optimize your code since it knows what c is at compile time. The only way to actually know would be to look at the generated assembly code to see if the branch was removed or not. That said, C++17 introduced constexpr if which is guaranteed to evaluate the condition at compile time and discard the branch that is not taken. That would make your function look like

template <bool c>
void func()
    // ...
    if constexpr(c) {
        // do something if c is true, discarded otherwise
    } else {
        // do something if c is false, discarded otherwise

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top