为什么constexpr不是自动的? [英] Why is constexpr not automatic?

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

问题描述

据我所知,constexpr可以看作是编译器的提示,以检查是否可以在编译时对给定的表达式求值.

As far as I understand it, constexpr can be seen as a hint to the compiler to check whether given expressions can be evaluated at compile-time and do so if possible.

我知道它也对声明为constexpr的函数或初始化施加了一些限制,但是最终目标是编译时评估,不是吗?

I know that it also imposes some restriction on the function or initialization declared as constexpr but the final goal is compile-time evaluation, isn't it?

所以我的问题是,为什么我们不能把它留给编译器?它显然可以检查前提条件,那么为什么不对每个表达式都做,并在可能的情况下在编译时求值呢?

So my question is, why can't we leave that at the compiler? It is obviously capable of checking the pre-conditions, so why doesn't it do for each expression and evaluate at compile-time where possible?

对于为什么会出现这种情况,我有两个想法,但是我还不相信它们能达到目的:

I have two ideas on why this might be the case but I am not yet convinced that they hit the point:

a)在编译期间可能会花费太长时间.

a) It might take too long during compile-time.

b)由于我的代码可以在不允许使用规范函数的位置使用constexpr函数,因此说明符也是声明的一部分.如果编译器自己完成所有操作,则可以在C数组定义中使用具有该功能一个版本的函数,但在下一版本中,则可能会出现编译器错误,因为编译时评估的前提条件是:更满意.

b) Since my code can use constexpr functions in locations where normale functions would not be allowed the specifier is also kind of part of the declaration. If the compiler did everything by itself, one could use a function in a C-array definition with one version of the function but with the next version there might be a compiler-error, because the pre-conditions for compile-time evaluation are no more satisfied.

推荐答案

constexpr 并不是编译器关于任何事情的提示". constexpr 要求.它并不需要在编译时实际执行表达式.它要求它可以.

constexpr is not a "hint" to the compiler about anything; constexpr is a requirement. It doesn't require that an expression actually be executed at compile time; it requires that it could.

constexpr (对于函数)的作用是限制您可以在函数定义中输入的内容,以便编译器可以在编译时轻松地执行该代码.这是程序员与编译器之间的合同.如果您的函数违反了合同,编译器将立即出错.

What constexpr does (for functions) is restrict what you're allowed to put into function definition, so that the compiler can easily execute that code at compile time where possible. It's a contract between you the programmer and the compiler. If your function violates the contract, the compiler will error immediately.

合同一旦建立,您现在可以在语言需要编译时间常数表达式的地方使用这些 constexpr 函数.然后,编译器可以检查常量表达式的元素,以查看表达式中的所有函数调用都调用了 constexpr 函数.如果没有,则再次导致编译器错误.

Once the contract is established, you are now able to use these constexpr functions in places where the language requires a compile time constant expression. The compiler can then check the elements of a constant expression to see that all function calls in the expression call constexpr functions; if they don't, again a compiler error results.

您尝试将其隐式化将导致两个问题.首先,如果没有该语言定义的明确约定,我怎么知道在 constexpr 函数中可以做什么和不能做什么?我怎么知道什么会使函数不是 constexpr ?

Your attempt to make this implicit would result in two problems. First, without an explicit contract as defined by the language, how would I know what I can and cannot do in a constexpr function? How do I know what will make a function not constexpr?

第二,在没有合同存在于编译器中的情况下,通过声明要创建函数 constexpr 的意图,编译器将如何验证我的函数符合该合同?不可能在我发现它实际上不是正确的 constexpr 函数之前,必须等到我在常量表达式中使用它.

And second, without the contract being in the compiler, via a declaration of my intent to make the function constexpr, how would the compiler be able to verify that my function conforms to that contract? It couldn't; it would have to wait until I use it in a constant expression before I find that it isn't actually a proper constexpr function.

最好明确地和事先说明合同.

Contracts are best stated explicitly and up-front.

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

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