- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我知道需求的差异,我最感兴趣的是它带来的代码质量带来的好处。
我能想到的几件事:
consteval
fns 在运行时从不使用(这是推测性的,我没有这方面的真实数据) constexpr
故障延迟到运行时:
constexpr int div_cx(int a, int b)
{
assert(b!=0);
return a/b;
}
int main()
{
static constexpr int result = div_cx(5,0); // compile time error, div by 0
std::cout << result;
std::cout << div_cx(5,0) ; // runtime error :(
}
最佳答案
为了获得有意义的、有意义的静态反射(编译时反射),您需要一种在编译时执行代码的方法。最初的静态反射 TS 提案使用了传统的模板元编程技术,因为它们是在编译时执行代码的唯一有效工具。
但是,随着 constexpr
代码获得更多功能,通过 constexpr
函数进行编译时静态反射变得越来越可行。这种想法的一个问题是不允许静态反射值泄漏到非编译时代码中。
我们需要能够编写只能在编译时执行的代码。对于函数中间的一小段代码,很容易做到这一点;该代码的运行时版本根本不包含反射部分,只包含它们的结果。
但是,如果您想编写一个接受反射值并返回反射值的函数呢?还是反射值列表?
该函数不能是 constexpr
,因为 constexpr
函数必须能够在运行时执行。您可以执行诸如获取指向 constexpr
函数的指针之类的操作,并以编译器无法跟踪的方式调用它们,从而强制它在运行时执行。
采用反射值的函数不能这样做。它必须仅在编译时执行。所以 constexpr
不适合这样的函数。
输入 consteval
:一个函数,即 "required" to execute only at compile time 。有一些特定的规则使指向此类函数的指针不可能泄漏到运行时代码等中。
因此,consteval
目前没有太多用途。它在 a few places like source_location::current()
, 中使用,从根本上说在运行时执行是没有意义的。但最终,该功能是进一步编译时编程工具尚不存在的必要构建块。
这是在 paper that originally proposed this feature 中规定的:
The impetus for the present paper, however, is the work being done by SG7 in the realm of compile-time reflection. There is now general agreement that future language support for reflection should use
constexpr
functions, but since "reflection functions" typically have to be evaluated at compile time, they will in fact likely be immediate functions.
关于c++ - 使用 consteval 代替 constexpr 函数有什么优点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64741639/
在 C++17 中,这段代码是非法的: constexpr int foo(int i) { return std::integral_constant::value; } 那是因为即使 fo
在下面的代码中,如果 here()不再是 consteval (完整的 RT 或 constexpr ),然后是 line()是 f() 的调用行里面 main() .但与 consteval这是f(
在 C++17 中,这段代码是非法的: constexpr int foo(int i) { return std::integral_constant::value; } 那是因为即使 fo
显然,consteval 将成为 C++20 中的关键字。 cppreference page因为它目前是空白的。它将是什么以及它与 constexpr 有何关系? 最佳答案 它声明了立即函数,即必须
我正在使用整洁的 fmt 库,在其版本 8 中,如果编译器支持相关功能,则会对其格式字符串进行编译时检查。 在某个时候,我想编写以下代码: throw my_exception("error: {}"
C++23即将引入if consteval .这将在哪里使用,它与 constexpr if 有何不同? ? 最佳答案 if consteval检测是否有 constexpr函数在常量表达式上下文中调
我有以下功能: template consteval size_t indexOf(SomeEnum someEnum, const std::ar
有时函数的结果不能用单个返回值来表示。例如:与两条线相交的函数。人们可能希望函数返回实际的交点以及它们之间的关系(即平行、相同、相交或倾斜)。 让我们假设这个例子,其中交点由某种类表示,线的位置关系由
这可能是一个愚蠢的问题,但我很困惑。我有一种感觉,立即(consteval)函数必须在编译时执行,而我们根本无法在二进制文件中看到它的主体。 This article明显支持我的感觉: This ha
有时函数的结果不能用单个返回值来表示。例如:与两条线相交的函数。人们可能希望函数返回实际的交点以及它们之间的关系(即平行、相同、相交或倾斜)。 让我们假设这个例子,其中交点由某种类表示,线的位置关系由
我在 C++20 模式下使用 GCC10 尝试了以下第一个变体: consteval std::experimental::source_location there() { return st
我正在编写一个类来包装与设备一起使用的基于 C 的库的一部分,其中每个设备都配置有回调函数指针来处理数据。将为每个设备创建一个 MyClass 实例。见下文: struct DeviceConfig
假设我们有一个 consteval 函数或一个带有 consteval 构造函数的普通结构,它只接受一些值: struct A { consteval A(int a) {
我很好奇就编译时评估而言,我可以将 gcc 推多远,所以我让它计算了 Ackermann函数,特别是输入值为 4 和 1(高于此值的任何值都是不切实际的): consteval unsigned in
C++20 将有新的 consteval关键字和constexpr destructor如果一切顺利。遗憾的是,据我所知,没有编译器实现 consteval此时。下面的代码有效吗? struct A
目前您不能使用 static_assert 来验证 constexpr 函数的参数,即使对它的所有调用确实都是 constexpr。这是有道理的,因为编译器仍然必须创建此函数的非 constexpr
我知道需求的差异,我最感兴趣的是它带来的代码质量带来的好处。 我能想到的几件事: reader 可以读取函数签名并知道函数在编译时被评估 编译器可能会发出更少的代码,因为 consteval fns
考虑以下代码。我可以用 GCC 10.2.0 和 Clang 11.0.0 编译它(正如预期的那样): #include template struct T { static constexpr
我正在尝试创建一个遵循构建器模式并在编译时完全运行的类(使用 C++20 中的新 consteval 关键字),但无论我尝试什么都行不通。例如,这将不起作用: #include class MyCl
我正在尝试创建一个遵循构建器模式并在编译时完全运行的类(使用 C++20 中的新 consteval 关键字),但无论我尝试什么都行不通。例如,这将不起作用: #include class MyCl
我是一名优秀的程序员,十分优秀!