为什么**不**声明一个函数为`constexpr`? [英] Whyever **not** declare a function to be `constexpr`?

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

问题描述

任何仅包含return语句的函数都可以声明为
constexpr ,因此,如果所有
参数都可以在编译时进行求值是 constexpr ,并且仅在其主体中调用 constexpr 函数。 是否有任何理由不声明任何这样的函数 constexpr

Any function that consists of a return statement only could be declared constexpr and thus will allow to be evaluated at compile time if all arguments are constexpr and only constexpr functions are called in its body. Is there any reason not to declare any such function constexpr ?

示例:

  constexpr int sum(int x, int y) { return x + y; }
  constexpr i = 10;
  static_assert(sum(i, 13) == 23, "sum correct");

谁能提供一个声明函数 constexpr的示例
会造成伤害吗?

一些最初的想法:

即使没有充分的理由声明函数
不是 constexpr 我也可以想象 constexpr 关键字具有
的过渡作用:在代码中不需要不需要编译时的
评估将允许不实现编译时间的编译器
评估仍可以编译该代码(但使用 constexpr 显式地使需要它们的代码
可靠地失败)。

Even if there should be no good reason for ever declaring a function not constexpr I could imagine that the constexpr keyword has a transitional role: its absence in code that does not need compile-time evaluations would allow compilers that do not implement compile-time evaluations still to compile that code (but to fail reliably on code that needs them as made explict by using constexpr).

但是我不明白的是:如果没有充分的理由让
声明一个不是 constexpr 的函数,为什么不是在声明为 constexpr 的标准库中每个函数
? (您不能认为
尚未完成,因为还没有足够的时间来完成
,因为对于 all 来说,这样做很容易-与之相反决定是否要使每个函数 constexpr 。)
---我知道 N2976
故意不需要许多标准库类型的cstr,例如
作为容器,因为这对于可能的
实现来说太过局限了。让我们从参数中排除它们,然后想一想:
在标准库中的类型实际上具有 constexpr cstr时,为什么不是对它操作的每个函数都声明了 constexpr

But what I do not understand: if there should be no good reason for ever declaring a function not constexpr, why is not every function in the standard library declared constexpr? (You cannot argue that it is not done yet because there was not sufficient time yet to do it, because doing it for all is a no-brainer -- contrary to deciding for every single function if to make it constexpr or not.) --- I am aware that N2976 deliberately not requires cstrs for many standard library types such as the containers as this would be too limitating for possible implementations. Lets exclude them from the argument and just wonder: once a type in the standard library actually has a constexpr cstr, why is not every function operating on it declared constexpr?

在大多数情况下,您也不能说您可能不希望声明一个函数 constexpr 仅仅是因为您不打算使用任何编译时用法:因为如果其他人使用evtl。将使用您的代码,他们可能会看到您没有使用的代码。 (但是当然也可以用于类型特征类型之类的东西。)

In most cases you also cannot argue that you may prefer not to declare a function constexpr simply because you do not envisage any compile-time usage: because if others evtl. will use your code, they may see such a use that you do not. (But granted for type trait types and stuff alike, of course.)

所以我想肯定有一个很好的理由和一个故意不声明函数的好例子 constexpr

So I guess there must be a good reason and a good example for deliberately not declaring a function constexpr?

(带有每个函数我总是说:每个满足
要求的函数 constexpr ,即被定义为单个
返回语句,仅采用constexpr
cstrs类型的参数,仅调用 constexpr 函数。自C ++ 14起,还有更多内容这样的函数体中允许的:例如, C ++ 14 constexpr函数可以使用局部变量和循环,因此可以将更广泛的函数类声明为 constexpr 。)

(with "every function" I always mean: every function that meets the requirements for being constexpr, i.e., is defined as a single return statement, takes only arguments of types with constexpr cstrs and calls only constexpr functions. Since C++14, much more is allowed in the body of such function: e.g., C++14 constexpr functions may use local variables and loops, so an even wider class of functions could be declared constexpr.)

问题为什么 std :: forward 丢弃 constexpr -ness?是这种情况的特例。

The question Why does std::forward discard constexpr-ness? is a special case of this one.

推荐答案

只能声明函数 constexpr 如果它们遵守 constexpr 的规则---不进行动态强制转换,不分配内存,不调用非- constexpr 函数,等等。

Functions can only be declared constexpr if they obey the rules for constexpr --- no dynamic casts, no memory allocation, no calls to non-constexpr functions, etc.

在标准库中将函数声明为 constexpr 要求所有实现都遵守这些规则。

Declaring a function in the standard library as constexpr requires that ALL implementations obey those rules.

首先,这需要检查每个可以 实现为 constexpr 的函数。

Firstly, this requires checking for each function that it can be implemented as constexpr, which is a long job.

第二,这对实现有很大的限制,并且会禁止许多调试实现。因此,只有在收益大于成本,或者要求足够严格,以至于实现几乎必须遵守 constexpr 规则时,才值得这样做。对每个功能进行此评估也是一项漫长的工作。

Secondly, this is a big constraint on the implementations, and will outlaw many debugging implementations. It is therefore only worth it if the benefits outweigh the costs, or the requirements are sufficiently tight that the implementation pretty much has to obey the constexpr rules anyway. Making this evaluation for each function is again a long job.

这篇关于为什么**不**声明一个函数为`constexpr`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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