std::is_constant_evaluated behavior

前端 未结 2 1669
半阙折子戏
半阙折子戏 2020-12-11 00:51

GCC9 already implements std::is_constant_evaluated. I played a little bit with it, and I realized it is somewhat tricky. Here’s my test:

constex         


        
2条回答
  •  醉酒成梦
    2020-12-11 01:24

    if constexpr requires a constant expression for a condition. So is_constant_evaluated is of course always going to be true in such a context.

    It's meant for a regular if. The purpose is to not go into a code path that is illegal in a constexpr function when evaluated in a constant expression. But to let it execute in runtime. It's not there to eliminate those code paths from the function altogether.

提交回复
热议问题