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

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

问题描述

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



。具有纯虚拟函数的接口

Boost.Function

最快的C ++代表

不可能快速的C ++代表

快速C ++代表

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

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


我的代理无法比较。未定义比较运算符,因为委托不包含指向方法的指针。指向存根函数的指针在各种编译单元中可以不同。


读者已经回复了:


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



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



如果您发现这对MSVC是真的,如果MSVC是标准授予,我会敦促您更新文章(包括比较功能)。


现在文章是四岁,作者在过去三年左右没有回复任何评论,所以我想知道是否对上述评论有什么好处,以及这种具体实施是否可以改变以支持比较。 C ++标准是否特别禁止这种使用,如果是,最近的编译器是否符合标准?



谢谢。

解决方案

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



因此,一个比较可以很好地定义,便携式方式。


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

. 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

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.

To which one the readers have replied:

"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))...

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.

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.

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?

Thanks.

解决方案

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天全站免登陆