Why disallow goto in constexpr functions?

前端 未结 2 956
小蘑菇
小蘑菇 2021-02-18 14:41

C++14 has rules for what you can and can\'t do in a constexpr function. Some of them (no asm, no static variables) seem pretty reasonable. But the Stan

2条回答
  •  星月不相逢
    2021-02-18 14:57

    constexpr is expected to be evaluated by the front-end of the compiler in some pseudo-interpreted mode or on the abstract syntax tree built in a single pass before any executable code is generated. We know that goto may jump to some part at the end of the function which was not evaluated yet. Hence properly connecting calling and executing goto would force building AST in several passes and jumping out of order between nodes in a tree is still a messy operation that could break some state. So such a construction is not worth the trouble.

提交回复
热议问题