快速C ++代理 [英] Fast C++ Delegates

查看:84
本文介绍了快速C ++代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道C ++代理的以下方法:

I'm aware of the following approaches to C++ delegates:

。与纯虚拟功能的接口

Boost.Function

最快可能的C ++代表

不可思议的快速C ++代表

快速C ++代表

快速C ++代理:Boost.Function'插件'替换和多播

. Interfaces with pure virtual functions
. Boost.Function
. The Fastest Possible C++ Delegates
. The Impossibly Fast C++ Delegates
. Fast C++ Delegates
. Fast C++ Delegate: Boost.Function 'drop-in' replacement and multicast

每个人都有自己的优缺点。有些更快,有些更灵活,有些更功能丰富,有些更符合标准,有些更便携,但我个人觉得自己喜欢第三个:Sergey Ryazanov的不可思议的快速C ++代表。问题是,他的代表是不可比的:

Each have their pros and cons. Some are faster, some are more flexible, some are more feature-rich, some are more standard-compliant and some are more portable, but I personally find myself fond of the third: Sergey Ryazanov's Impossibly Fast C++ Delegates. The problem though, is that his delegates are not comparable:


我的代表无法比较。没有定义比较运算符,因为委托不包含指向方法的指针。

My delegates cannot be compared. Comparison operators are not defined because a delegate doesn't contain a pointer to method. Pointer to a stub function can be different in various compilation units.

读者回答了哪一个:


在各种编译单元中,存根功能的指针可能不同。
AFAIK,这不是真的。编译器需要重新使用不同编译单元生成的模板函数(我相信 - 但是我认为Borland曾经违反了这个规则)。我认为这是因为类(不在无名命名空间中)使用外部链接,并且使用存根函数的方式将始终防止它们被内联(尽管这不应该是一个问题,而是使用函数的地址将强制生成非内联版本,链接器执行的外部链接将消除所有除了一个类似命名的函数(它们被假设并要求与标准相同))...

"Pointer to a stub function can be different in various compilation units." AFAIK, this is not true. Compilers are required to re-use template functions generated in different compilation units (this I am sure of - but I think Borland once violated this rule). I think it is because classes (ones not in 'nameless' namespaces) use external linkage and the way you use the stub functions will always prevent them from being inlined (although this shouldn't be an issue either as taking the address of the function will force a non-inline version to be generated and 'external linkage' performed by the linker will eliminate all but one similarly named function (they are assumed and required to be identical by the standard))...

如果您定义一个模板函数一个翻译单元(cpp文件),然后在另一个翻译单元中定义相同的函数,则只有两个版本中的一个将使其进入最终的可执行文件。 (这实际上违反了一个定义规则,但是在GCC上起作用,至少...不确定MSVC。)要点是:[存根]的地址将以不同的单位相同。

If you define a template function one translation unit (cpp file) and then define the same function differently in another translation unit, only one of the two versions will make it into the final executable. (This actually violates the "One Definition Rule", but works on GCC, at least... not sure about MSVC.) The point is: the address [of the stub] will be the same in different units.

如果您认为MSVC是正确的,那么MSVC是否符合标准要求,我会敦促您更新文章(包括比较功能)。

I would urge you to update the article (including comparison capability) if you find this to be true for MSVC - if MSVC is standards conferment, in this regard.

现在这篇文章是四岁,作者没有回复过去三年左右的任何评论,所以我想知道上述评论有什么优点,这个具体实施是否可以改变,以支持比较。 C ++标准是否明确禁止此类使用,如果是这样,最近的编译器是否符合实际标准?

Now the article is four years old and the author hasn't replied to any of the comments during the past three years or so, so I'm wondering if there's any merit to the above comment and whether this specific implementation can indeed be changed to support comparisons. Does the C++ standard specifically prohibit such usage and if so, are any of the recent compilers actually standard-compliant in that regard?

谢谢。

推荐答案

代码既符合标准,也符合要求。我没有看到他违反ODR的任何地方,而且具有相同模板参数的功能模板的所有实例都应该具有相同的地址(在某种意义上说,函数的指针应该都是相等的) - 如何这个实现并不重要。 ISO C ++ 03 14.5.5.1 [temp.over.link]更详细地描述了这些规则。

The code is both standard compliant, and fine. I don't see any place where he violates ODR, and it is true that all instantiations of a function template with the same template parameters should have "the same address" (in a sense that pointers to functions should all be equal) - how this is achieved is not important. ISO C++03 14.5.5.1[temp.over.link] describes the rules in more detail.

所以,一个比较可以很好地定义在一个符合和便携式

So, a comparison could well be defined there in a conformant and portable way.

这篇关于快速C ++代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆