What do compilers do with compile-time branching?

前端 未结 5 1098
生来不讨喜
生来不讨喜 2020-11-28 07:23

EDIT: I took the \"if/else\" case as an example that can sometimes be resolved at compile time (eg when static values are involved, cf

5条回答
  •  温柔的废话
    2020-11-28 07:50

    The compiler may be smart enough to see that it can replace the if statement body with two different function implementations, and just choose the right one. But as of 2014 I doubt there is any compiler that is smart enough to do that. I may be wrong though. On second thought, std::is_integral is simple enough that I think it will be optimized away.

    Your idea of overloading on the result of std::is_integral is one possible solution.

    Another and IMHO cleaner solution is to use std::enable_if (together with std::is_integral).

提交回复
热议问题