C++ 什么是“轰”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2641489/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is a 'thunk'?
提问by fbrereto
I've seen it used in programming (specifically in the C++ domain) and have no idea what it is. Presumably it is a design pattern, but I could be wrong. Can anyone give a good example of a thunk?
我见过它在编程中使用(特别是在 C++ 域中),但不知道它是什么。大概这是一种设计模式,但我可能是错的。任何人都可以举一个 thunk 的好例子吗?
采纳答案by Chris Dodd
A thunk
usually refers to a small piece of code that is called as a function, does some small thing, and then JUMP
s to another location (usually a function) instead of returning to its caller. Assuming the JUMP target is a normal function, when it returns, it will return to the thunk's caller.
Athunk
通常是指作为函数调用的一小段代码,做一些小事情,然后JUMP
s 到另一个位置(通常是一个函数)而不是返回到它的调用者。假设 JUMP 目标是一个普通函数,当它返回时,它会返回给 thunk 的调用者。
Thunks can be used to implement lots of useful things efficiently
Thunks 可以用来高效地实现很多有用的东西
protocol translation -- when calling from code that uses one calling convention to code that uses a different calling convention, a
thunk
can be used to translate the arguments appropriately. This only works if the return conventions are compatible, but that is often the casevirtual function handling -- when calling a virtual function of a multiply-inherited base class in C++, there needs to be a fix-up of the
this
pointer to get it to point to the right place. Athunk
can do this.dynamic closures -- when you build a dynamic closure, the closure function needs to be able to get at the context where it was created. A small
thunk
can be built (usually on the stack) which sets up the context info in some register(s) and then jumps to a static piece of code that implements the closure's function. The thunk here is effectively supplying one or more hidden extra arguments to the function that are not provided by the call site.
协议转换——当从使用一种调用约定的代码调用到使用不同调用约定的代码时,a
thunk
可用于适当地转换参数。这仅在返回约定兼容时才有效,但情况通常如此虚函数处理——在 C++ 中调用多重继承基类的虚函数时,需要修复
this
指针以使其指向正确的位置。Athunk
可以做到这一点。动态闭包——当你构建一个动态闭包时,闭包函数需要能够获得创建它的上下文。
thunk
可以构建一个小程序(通常在堆栈上),它在某些寄存器中设置上下文信息,然后跳转到实现闭包功能的静态代码段。这里的 thunk 有效地为函数提供了一个或多个隐藏的额外参数,这些参数不是由调用站点提供的。
回答by Robert Harvey
The word thunk has at least three related meanings in computer science. A "thunk" may be:
thunk 这个词在计算机科学中至少有三个相关的含义。“thunk”可能是:
- a piece of code to perform a delayed computation (similar to a closure)
- a feature of some virtual function table implementations (similar to a wrapper function)
- a mapping of machine data from one system-specific form to another, usually for compatibility reasons
- 一段执行延迟计算的代码(类似于闭包)
- 一些虚函数表实现的特性(类似于包装函数)
- 机器数据从一种系统特定形式到另一种形式的映射,通常是出于兼容性原因
I have usually seen it used in the third context.
我通常看到它用于第三种情况。
回答by Ivan Godard
The term thunk originally referred to the mechanism used by the Royal Radar Establishmentimplementation of pass-by-name in their Algol60compiler. In general it refers to any way to induce dynamic behavior when referencing an apparently static object. The term was invented by Brian Wichmann, who when asked to explain pass-by-name said "Well you go out to load the value from memory and then suddenly - thunk - there you are evaluating an expression."
术语 thunk 最初是指皇家雷达机构在其Algol60编译器中实现按名称传递的机制。一般而言,它指的是在引用明显静态的对象时引发动态行为的任何方式。这个术语是由 Brian Wichmann 发明的,当被要求解释传递名称时,他说:“好吧,你出去从内存中加载值,然后突然 - thunk - 你正在评估一个表达式。”
Thunks have been put in hardware (cf. KDF9, Burroughs mainframes). There are several ways to implement them in software, all very machine, language and compiler specific.
Thunk 已被放入硬件中(参见 KDF9,Burroughs 大型机)。有几种方法可以在软件中实现它们,所有方法都非常特定于机器、语言和编译器。
The term has come to be generalized beyond pass-by-name, to include any situation in which an apparently or nominally static data reference induces dynamic behavior. Related terms include "trampoline" and "future".
该术语已经超出了按名称传递的范围,包括任何明显或名义上的静态数据引用引起动态行为的情况。相关术语包括“蹦床”和“未来”。
回答by OscarRyz
Some compilers for object-oriented languages such as C++ generate functions called "thunks" as an optimization of virtual function calls in the presence of multiple or virtual inheritance.
一些面向对象语言(例如 C++)的编译器生成称为“thunk”的函数,作为存在多重继承或虚继承的情况下对虚函数调用的优化。
Taken from: http://en.wikipedia.org/wiki/Thunk#Thunks_in_object-oriented_programming
摘自:http: //en.wikipedia.org/wiki/Thunk#Thunks_in_object-oriented_programming
回答by Jon Davis
This question has already been asked on SO, see:
这个问题已经在 SO 上问过了,请参阅:
What is a 'thunk', as used in Scheme or in general?
什么是“thunk”,在 Scheme 中或一般情况下使用?
From what I can tell, it's akin to a lambda statement, where you may not want to return the value until you need to evaluate it; or it can also be compared to a property getter which by design executes some code in order to return a value while yet having the interface form that comes across more like a variable, but also has polymorphic behavior that can be swapped out whether by inheritance or by swapping out the function pointer that would evaluate and return a value at runtime based on compile-time or environmental characteristics.
据我所知,它类似于 lambda 语句,您可能不想在需要评估它之前返回该值;或者也可以将它比作一个属性 getter,它按照设计执行一些代码以返回一个值,同时具有更像一个变量的接口形式,但也具有多态行为,可以通过继承或通过换出将在运行时根据编译时或环境特征评估和返回值的函数指针。
回答by Jerry Coffin
There's considerable variation in use. Almost universally, a thunk is a function that's (at least conceptually) unusually small and simple. It's usually some sort of adapter that gives you the correct interface to something or other (some data, another function, etc.) but is at least seen as doing little else.
使用上有相当大的差异。几乎普遍而言,thunk 是一个(至少在概念上)异常小而简单的函数。它通常是某种适配器,可以为您提供正确的接口(某些数据、另一个功能等),但至少被视为没有其他作用。
It's almost like a form of syntactic sugar, except that (at least as usually used) syntactic sugar is supposed to make things look the way the human reader wants to see them, and a thunk is to make something look the way the compiler wants to see it.
它几乎就像一种语法糖,除了(至少像通常使用的那样)语法糖应该使事物看起来像人类读者希望看到的那样,而 thunk 是使某些事物看起来像编译器希望的那样看见。
回答by MusiGenesis
I'm going to look this up, but I thought thunkingwas the process employed by a 32-bit processor to run legacy 16-bit code.
我打算查一下,但我认为thunking是 32 位处理器用来运行遗留 16 位代码的过程。
I used to use it as an analogy for how you have to restrict how fast you talk and what words you use when talking to dumb people.
我曾经用它来比喻你必须限制你说话的速度和你在与愚蠢的人交谈时使用的词。
Yeah, it's in the Wikipedia link (the part about 32-bit, not my nerdalogy).
是的,它在维基百科链接中(关于 32 位的部分,不是我的书呆子)。
https://en.wikipedia.org/wiki/Thunk
https://en.wikipedia.org/wiki/Thunk
Much of the literature on interoperability thunks relates to various Wintel platforms, including MS-DOS, OS/2,[8]Windows[9][10] and .NET, and to the transition from 16-bit to 32-bit memory addressing. As customers have migrated from one platform to another, thunks have been essential to support legacy software written for the older platforms.
许多关于互操作性 thunk 的文献都与各种 Wintel 平台有关,包括 MS-DOS、OS/2、[8]Windows[9][10] 和 .NET,以及从 16 位到 32 位内存寻址的过渡. 随着客户从一个平台迁移到另一个平台,thunk 对于支持为旧平台编写的遗留软件至关重要。
(emphasis added by me)
(重点是我加的)
回答by u1005791
I was distressed to find no general 'computer science' definition of this term matching its de-facto usage as known historically to me. The first real-life encounter I can recall where it was actually called that was in the OS/2 days and the 16-32 bit transition. It appears "thunking" is like irony in its application today.
我很苦恼地发现这个术语的一般“计算机科学”定义与我在历史上已知的事实上的用法相匹配。我能回忆起的第一次现实生活中它的实际调用是在 OS/2 天和 16-32 位转换中。在今天的应用中,“thunking”似乎具有讽刺意味。
My rough general understanding is that the thunk is a stub routine that just does nothing or routes across some fundamental boundary in kind between systems as in the mentioned historical cases.
我粗略的一般理解是,thunk 是一个存根例程,它什么也不做,或者跨越系统之间的某种基本边界,就像在提到的历史案例中一样。
So the sense is like a synesthesia of being dropped from the one environment to the other making (metaphorically/as a simile) a "thunk" sound.
因此,这种感觉就像是从一个环境掉到另一个环境中的联觉(比喻/作为明喻)发出“thunk”的声音。
回答by Ivan Godard
The earliest use of "thunk" I know of is from late '50s in reference to Algol60 pass-by-name argument evaluation in function calls. Algol was originally a specification language, not a programming language, and there was some question about how pass-by-name could be implemented on a computer.
我所知道的最早使用“thunk”是从 50 年代后期开始,参考 Algol60 函数调用中的按名称传递参数评估。Algol 最初是一种规范语言,而不是一种编程语言,关于如何在计算机上实现按名称传递存在一些问题。
The solution was to pass the entry point of what was essentially a lambda. When the callee evaluated the parameter, control fell through - thunk! - into the caller's context where the lambda was evaluated and it's result became the value of the parameter in the callee.
解决方案是通过本质上是 lambda 的入口点。当被调用者评估参数时,控制失败了 - thunk!- 进入调用者的上下文,其中 lambda 被评估,其结果成为被调用者中参数的值。
In tagged hardware, such as the Burroughs machines, the evaluation was implicit: an argument could be passed as a data value as in ordinary pass-by-value, or by thunk for pass-by-name, with different tags in the argument metadata. A load operation hardware checked the tag and either returned the simple value or automatically invoked the lambda thunk.
在标记硬件中,例如 Burroughs 机器,评估是隐式的:参数可以作为数据值传递,就像普通的值传递一样,或者通过 thunk 传递名称,在参数元数据中使用不同的标签. 加载操作硬件检查标签并返回简单值或自动调用 lambda thunk。
回答by l--''''''---------''''''''''''
Per Kyle Simpson'sdefinition, a thunkis a way to abstract the component of timeout of asynchronous code.
根据Kyle Simpson 的定义,thunk是一种从异步代码中抽象出时间组件的方法。