为什么在constexpr函数中不允许goto? [英] Why disallow goto in constexpr functions?

查看:98
本文介绍了为什么在constexpr函数中不允许goto?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++ 14对于constexpr函数中可以做什么和不能做什么都有规则.其中一些(没有asm,没有静态变量)似乎很合理.但是,即使允许其他控制流机制,该标准也不允许在constexpr函数中使用goto.
这种区别背后的原因是什么?
我以为我们过去了" goto对于编译器来说很难".

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 Standard also disallows goto in constexpr functions, even while it allows other control flow mechanisms.
What's the reasoning behind this distinction?
I thought we were past "goto is hard for compilers".

推荐答案

我的理解是希望在C ++ 14中获得轻松的constexpr语义.放宽了很多限制,这些限制很简单,但是有些争议性更大或更困难,或者[在这里插入您自己选择的形容词].与其仅仅为了使用goto的能力而坚持宽松的constexpr,还不如仅仅发布主要更改并保留其余内容.这似乎是一个不错的选择,因为C ++ 14中的constexpr比C ++ 11中的constexpr强大得多,考虑到所有因素,无法使用goto的可能性很小.

My understanding is there was a desire to get relaxed constexpr semantics in C++14. A lot of the restrictions that were relaxed were straightforward, but some were more controversial or difficult or [insert adjective of your choice here]. Rather than hold up relaxed constexpr just for the ability to use goto, it was decided to just publish the main changes and hold off on the rest. This seems like a pretty sound choice, since constexpr in C++14 is far more powerful than constexpr in C++11, and not being able to use goto is a fairly minor absence, all things considered.

也就是说,确实存在这样一种观点,即在constexpr上下文中包含goto既有用又可能.实际上,初始提案可以放松constexpr允许它.因此,也许所要做的就是有人希望它编写一份提案以添加它. 显然是两年前有人Ville Voutilainen在

That said, there certainly exists the view that having goto in constexpr contexts is both useful and possible. Indeed, the initial proposal for relaxing constexpr allowed it. So maybe all it takes is somebody that wants it to write a proposal to add it. That somebody could be you! was apparently Ville Voutilainen two years ago in N4472, which featured the quite-relevant-to-this-question paragraph of:

有一种没有根据的传闻,即禁止在常量表达式中使用goto的原因更多是出于口味而不是技术原因,这意味着在常量表达式中支持goto并不是特别容易实现.我不能说这对于一般的实现是否正确.

There is unsubstantiated hearsay according to which banning goto in constant expressions is more for taste reasons than technical reasons, meaning that supporting goto in constant expressions isn't particularly hard to implement. I can't say whether that's correct for implementations in general.

该论文的接受程度参差不齐,但是现在我们有了constexpr lambdas,也许需要重新审视它.而那个有人可能是你!

The paper had mixed reception, but now that we have constexpr lambdas, maybe it needs to be revisited. And that somebody could be you!

这篇关于为什么在constexpr函数中不允许goto?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆