为什么C ++ 11 constexpr这么限制? [英] Why is C++11 constexpr so restrictive?

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

问题描述

您可能知道,C ++ 11引入了 constexpr 关键字。

As you probably know, C++11 introduces the constexpr keyword.


C ++ 11引入了关键字constexpr,它允许用户
保证函数或对象构造函数是编译时
常量。
[...]
这允许编译器理解和验证[function name]是一个
编译时常量。

C++11 introduced the keyword constexpr, which allows the user to guarantee that a function or object constructor is a compile-time constant. [...] This allows the compiler to understand, and verify, that [function name] is a compile-time constant.

我的问题是为什么有可以声明的函数的形式有严格的限制。我理解希望保证函数是纯的,但是要考虑这一点:

My question is why are there such strict restrictions on form of the functions that can be declared. I understand desire to guarantee that function is pure, but consider this:


对函数使用constexpr对
函数可以做。首先,函数必须有一个非void返回
类型。第二,函数体不能声明变量或者定义新的
类型。第三,正文只能包含声明,null语句
和一个return语句。必须存在参数值
,在参数替换后,返回
语句中的表达式产生一个常量表达式。

The use of constexpr on a function imposes some limitations on what that function can do. First, the function must have a non-void return type. Second, the function body cannot declare variables or define new types. Third, the body may only contain declarations, null statements and a single return statement. There must exist argument values such that, after argument substitution, the expression in the return statement produces a constant expression.

这意味着这个纯函数是非法的:

That means that this pure function is illegal:

constexpr int maybeInCppC1Y(int a, int b)
{
    if (a>0)
        return a+b;
    else
        return a-b;
  //can be written as   return  (a>0) ? (a+b):(a-b); but that isnt the point
}


所以我想知道这是一个设计决策,还是编译器吸吮,当涉及到证明函数a是纯的?

Also you cant define local variables... :( So I'm wondering is this a design decision, or do compilers suck when it comes to proving function a is pure?

推荐答案

你需要写语句而不是表达式的原因是你想利用语句的附加功能,特别是循环的能力。但是有用的是,需要声明变量也禁止)。

The reason you'd need to write statements instead of expressions is that you want to take advantage of the additional capabilities of statements, particularly the ability to loop. But to be useful, that would require the ability to declare variables (also banned).

如果你结合一个循环设施,可变变量,逻辑分支(如 if 语句),那么您就有能力创建无限循环。无法确定这样的循环是否会终止(暂停问题因此一些源会导致编译器挂起。

If you combine a facility for looping, with mutable variables, with logical branching (as in if statements) then you have the ability to create infinite loops. It is not possible to determine if such a loop will ever terminate (the halting problem). Thus some sources would cause the compiler to hang.

通过使用递归纯函数,可能导致无限递归,可以显示为相当于上述的循环能力。但是,C ++在编译时已经有这个问题 - 它通过模板扩展发生 - 因此编译器必须有一个模板栈深度的开关,所以他们知道什么时候放弃。

By using recursive pure functions it is possible to cause infinite recursion, which can be shown to be equivalently powerful to the looping capabilities described above. However, C++ already has that problem at compile time - it occurs with template expansion - and so compilers already have to have a switch for "template stack depth" so they know when to give up.

所以这些限制似乎旨在确保这个问题(确定一个C ++编译是否会完成)不会比它已经有任何棘手。

So the restrictions seem designed to ensure that this problem (of determining if a C++ compilation will ever finish) doesn't get any thornier than it already is.

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

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