C++ 中的多重调度
我想了解什么是多重调度。我读了很多不同的文本,但我仍然不知道多重调度是什么以及它有什么好处。也许我缺少的是使用多重调度的代码片段。请问,您能否使用多重分派在 C++ 中编写一小段代码,以便我可以看到它无法正确编译/运行,因为 C++ 只有单分派?我需要看看其中的区别。谢谢。
I am trying to understand what multiple dispatch is. I read a lot of various texts but I still have no idea what multiple dispatch is and what it is good for. Maybe the thing I am missing is piece of code using multiple dispatch. Please, can you write a little piece of code in C++ using multiple dispatch so that I can see it cannot be compiled/runned properly because C++ has only single dispatch? I need to see the difference. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
多重调度是根据传递给函数调用的参数的运行时类型来选择要调用的函数版本的能力。
这是一个在 C++ 中无法正常工作的示例(未经测试):
Multi-dispatch is the ability to choose which version of a function to call based on the runtime type of the arguments passed to the function call.
Here's an example that won't work right in C++ (untested):
多重分派是指执行的函数取决于多个对象的运行时类型。
C++ 具有单一调度,因为当您使用虚函数时,实际运行的函数仅取决于 -> 左侧对象的运行时类型。或者 。操作员。
我正在努力思考多重调度的真实编程案例。也许是在一个不同角色互相战斗的游戏中。
一场战斗的胜利者可能取决于两个对手的特征,因此您可能希望根据两个参数的运行时类型将此调用分派到以下其中一个:
该函数的作用取决于两个参数的类型,不只是一个。在 C++ 中,您可能必须将其编写为一些虚拟函数。将根据一个参数(this 指针)来选择虚拟函数。然后,虚拟函数可能需要包含一个开关或某些东西来对另一个参数执行特定的操作。
Multiple dispatch is when the function that gets executed depends on the run time type of more than one object.
C++ has single dispatch because when you use virtual functions, the actual function that gets run depends only on the run-time type of the object to the left of the -> or . operator.
I'm struggling to think of a real programming case for multiple dispatch. Maybe in a game where various characters fight each other.
The winner of a fight may depend on the characteristics of both opponents, so you may want this call to dispatch to one of the following, depending on the run-time types of both arguments:
What the function does depends on the types of both arguments, not just one. In C++ you might have to write this as some virtual functions. A virtual function would be selected depending on one argument (the this pointer). Then, the virtual function may need to contain a switch or something to do something particular to the other argument.
在单次调度中,执行的函数仅取决于对象类型。在
双重调度执行的函数取决于对象类型和
范围。
在以下示例中,使用以下命令调用函数
Area()
单次调度,而
Intersect()
依赖于双次调度,因为它需要形状参数。
该示例基于这篇文章。
In single dispatch the function executed depends on just the object type. In
double dispatch the function executed depends on the object type and a
parameter.
In the following example, the function
Area()
is invoked usingsingle dispatch, and
Intersect()
relies on double dispatch because it takes aShape parameter.
The example is based on this article.
从 2022 年的角度来看,有相当多的技术可以在 C++ 中进行多重调度,
这是一种带有动态转换的技术(在撰写本文时已经可用):
https://gist.github.com/jspahrsummers/0834f93cac6d5efa2418ddddf7244b16
这个具有更新的元编程东西:
https://arne-mertz.de/2019/10 /multiple-dispatch-over-covariant-functions/
如果你将其与 julia 等进行比较,它会更复杂,并且以某种方式它可能会产生一些性能开销(需要澄清多少)。这可能会因 C++ 编译器而异,最终 Julia 的 LLVM 也将使用某种形式的 RTTI。
不确定在最新最好的 C++20 中是否可以做得更好。
To put in perspective in 2022 there are quite a few techniques to do multi-dispatch in C++
This one with dynamic casting (already available at the time of this post):
https://gist.github.com/jspahrsummers/0834f93cac6d5efa2418ddddf7244b16
And this one with more recent metaprogramming stuff:
https://arne-mertz.de/2019/10/multiple-dispatch-over-covariant-functions/
If you compare this to julia or such it is more complex and one way or another it may have some performance overhead (to be clarified how much). This may vary depending on the C++ compiler, at the end of the day the LLVM of julia will also use some form of RTTI.
Not sure if in latest and greatest C++20 you can do better.