gpt4 book ai didi

c++ - 难以置信的快速 C++ 委托(delegate)和不同的翻译单元

转载 作者:IT老高 更新时间:2023-10-28 21:58:01 25 4
gpt4 key购买 nike

根据 Sergey Ryazanov 的说法,他的 Impossibly Fast C++ Delegates没有可比性:

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.

读者回复了哪一篇:

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

现在文章四年了,作者在过去三年左右没有回复任何评论,所以我想知道上面的评论是否有任何优点,这个具体实现是否确实可以进行更改以支持比较。

C++ 标准是否明确禁止此类使用,如果是,最新的编译器在这方面是否真正符合标准?

最佳答案

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

因此,可以以一致且可移植的方式在那里定义比较。

关于c++ - 难以置信的快速 C++ 委托(delegate)和不同的翻译单元,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1526912/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com