【问题标题】:Is there a non-indirection, non-hack way to guarantee that a constexpr function only be callable at compile time?是否有一种非间接、非 hack 的方式来保证 constexpr 函数只能在编译时调用?
【发布时间】:2019-11-26 19:07:51
【问题描述】:

目前,我们有两个主要的编译时评估选项:模板元编程(通常使用模板结构和/或变量)和constexpr 操作1

template<int l, int r> struct sum_ { enum { value = l + r }; }; // With struct.
template<int l, int r> const int sum = sum_<l, r>::value;       // With struct & var.
template<int l, int r> const int sub = l - r;                   // With var.
constexpr int mul(int l, int r) { return l * r; }               // With constexpr.

我们保证所有四个都可以在编译时进行评估。

template<int> struct CompileTimeEvaluable {};

CompileTimeEvaluable<sum_<2, 2>::value> template_struct;                 // Valid.
CompileTimeEvaluable<sum<2, 2>>         template_struct_with_helper_var; // Valid.
CompileTimeEvaluable<sub<2, 2>>         template_var;                    // Valid.
CompileTimeEvaluable<mul(2, 2)>         constexpr_func;                  // Valid.

由于模板的编译时特性,我们还可以保证前三个在编译时可评估;但是,我们不能为 constexpr 函数提供同样的保证。

int s1 = sum_<1, 2>::value;
//int s2 = sum_<s1, 12>::value; // Error, value of i not known at compile time.

int sv1 = sum<3, 4>;
//int sv2 = sum<s1, 34>;        // Error, value of i not known at compile time.

int v1 = sub<5, 6>;
//int v2 = sub<v1, 56>;         // Error, value of i not known at compile time.

int c1 = mul(7, 8);
int c2 = mul(c1, 78);           // Valid, and executed at run time.

use indirection to provide an effective guarantee that a given constexpr function can only be called at compile time 是可能的,但如果直接访问函数而不是通过间接帮助程序(如链接答案的 cmets 中所述)访问该函数,则此保证会失效。也可以poison a constexpr function 这样在运行时调用它就变得不可能了,by throwing an undefined symbol,因此通过笨拙的 hack 提供了这种保证。然而,这些似乎都不是最佳选择。


考虑到这一点,我的问题是:包括当前标准、C++20 草案、正在考虑的提案、实验性功能以及任何其他类似的东西,有没有一种方法可以在不诉诸黑客或间接手段的情况下提供这种保证,仅使用内置和/或考虑内置到语言本身的功能和工具? [例如,一个属性,如(理论)[[compile_time_only]][[no_runtime]]std::is_constant_evaluated 的用法,或者一个概念,也许?]

1:宏技术上也是一种选择,但是...是的,不。

【问题讨论】:

标签: c++ constexpr


【解决方案1】:

C++20 为这个明确的目的添加了constevalconsteval 函数是保证仅在编译时调用的 constexpr 函数。

【讨论】:

  • 哦,看起来很有趣。基于固有的inline 规范和每次调用都产生一个常量表达式的要求,consteval 函数通常会在编译期间被完全优化,这是否是一个有效的假设?
  • @justintime 是正确的。它将在编译时进行评估,并且二进制文件中只保留一个常量。
猜你喜欢
  • 2017-06-15
  • 2018-07-10
  • 1970-01-01
  • 2013-12-24
  • 1970-01-01
  • 1970-01-01
  • 2020-07-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多