本文介绍了“中毒一个功能”是什么意思?在C ++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Scott Schurr的演讲末尾,在CppCon介绍中未解析的 extern / li>

  1. Putting a throw in a constexpr function
  2. Declaring an unresolved extern const char*
  3. Referencing the unresolved extern in the throw

我觉得我的深度有点偏差,但我很好奇:

I sense that I'm a bit out of my depth here, but I'm curious:

推荐答案

通常指的是使函数不可用,例如如果你想禁止在程序中使用动态分配,你可以毒化 malloc 函数,因此它不能使用。

In general it refers to making a function unusable, e.g. if you want to ban the use of dynamic allocation in a program you could "poison" the malloc function so it can't be used.

在视频中,他以更具体的方式使用它,这是清楚的,如果你阅读显示的幻灯片,当他谈到中毒的功能,这说一种方式强制编译时间? 。

In the video he's using it in a more specific way, which is clear if you read the slide that is displayed when he talks about poisoning the function, which says "A way to force compile-time only?"

因此,他在谈论中毒函数,使其在运行时不可用,因此在常量表达式中只有 可调用。该技术是在函数中有一个分支,当在编译时上下文中被调用时,该分支不会被执行,并且使该分支包含会导致错误的东西。

So he is talking about "poisoning" the function to make it uncallable at run-time, so it's only callable in constant expressions. The technique is to have a branch in the function which is never taken when called in a compile-time context, and to make that branch contain something that will cause an error.

在constexpr函数中允许使用一个 throw 表达式,只要在函数的编译时调用期间永远不会到达它(因为你不能在编译时抛出异常 - 时间,它是一个固有的动态操作,如分配内存)。因此,引用未定义符号的throw表达式不会在编译期调用期间使用(因为这将无法编译),并且不能在运行时使用,因为未定义的符号会导致链接器错误。

A throw expression is allowed in a constexpr function, as long as it is never reached during compile-time invocations of the function (because you can't throw an exception at compile-time, it's an inherently dynamic operation, like allocating memory). So a throw expression that refers to an undefined symbol will not be used during compile-time invocations (because that would fail to compile) and cannot be used at run-time, because the undefined symbol causes a linker error.

因为未定义的符号在函数的编译时调用中不是odr-used,所以实际上编译器不会创建对符号的引用,因此它是未定义的。

Because the undefined symbol is not "odr-used" in the compile-time invocations of the function, in practice the compiler will not create a reference to the symbol, so it's OK that it's undefined.

这很有用吗?他正在演示如何如何做,不一定说这是一个好主意或广泛有用。如果你有某种原因需要做它的技术可能会解决你的问题。

Is that useful? He's demonstrating how to do it, not necessarily saying it's a good idea or widely useful. If you have a need to do it for some reason then his technique might solve your problem. If you don't have a need for it, you don't need to worry about it.

有用的原因之一是:当一些操作的编译时版本不是尽可能高效时。对constexpr函数允许的表达式类型有限制(特别是在C ++ 11中,在C ++ 14中删除了一些限制)。因此,您可能有两个版本的函数执行计算,一个是最佳的,但使用的是在constexpr函数中不允许的表达式,一个是有效的constexpr函数,但是如果在run-时间。你可以毒化次优的,以确保它从来不用于运行时调用,确保更高效的(非constexpr)版本用于运行时调用。

One reason it might be useful is when the compile-time version of some operation is not as efficient as it could be. There are restrictions on the kind of expressions allowed in a constexpr function (especially in C++11, some restrictions were removed in C++14). So you might have two versions of a function for performing a calculation, one that is optimal, but uses expressions that aren't allowed in a constexpr function, and one that is a valid constexpr function, but would perform poorly if called at run-time. You could poison the sub-optimal one to ensure it is never used for run-time calls, ensuring the more efficient (non-constexpr) version is used for run-time calls.

注意在编译期使用的constexpr函数的性能并不重要,因为它没有运行时开销。它可能会使编译器执行额外的工作,从而减慢编译速度,但不会产生任何运行时性能成本。

N.B. The performance of a constexpr function used at compile-time is not really important, because it has no run-time overhead anyway. It might slow down your compilation by making the compiler do extra work, but it won't have any run-time performance cost.

这篇关于“中毒一个功能”是什么意思?在C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!