gpt4 book ai didi

c++ - 为什么在 constexpr 函数中不允许 goto?

转载 作者:IT老高 更新时间:2023-10-28 21:36:35 26 4
gpt4 key购买 nike

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

最佳答案

我的理解是希望在 C++14 中获得放松的 constexpr 语义。许多放宽的限制很简单,但有些更具争议性或困难性或[在此处插入您选择的形容词]。与其为了使用 goto 的能力而保留宽松的 constexpr,不如决定只发布主要更改并推迟其余的更改。这似乎是一个不错的选择,因为 C++14 中的 constexpr 比 C++11 中的 constexpr 强大得多,而且无法使用 考虑到所有因素,goto 是一个相当小的缺席。

也就是说,确实存在这样一种观点,即在 constexpr 上下文中使用 goto 既有用又可能。确实,initial proposal为了放松 constexpr 允许它。因此,也许只需要有人希望它写一个提案来添加它。那个人可能是你!显然是两年前 N4472 中的 Ville Voutilainen ,其中包含与此问题非常相关的段落:

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 lambda,也许需要重新审视它。 那个有人可能是你!

关于c++ - 为什么在 constexpr 函数中不允许 goto?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45266577/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com