简体   繁体   English

不可能快速的C ++代表和不同的翻译单元

[英]Impossibly Fast C++ Delegates and different translation units

According to Sergey Ryazanov, his Impossibly Fast C++ Delegates are not comparable: 根据Sergey Ryazanov的说法,他不可能快速的C ++代表不具备可比性:

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. AFAIK,这不是真的。 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). 编译器需要重复使用在不同编译单元中生成的模板函数(我确信 - 但我认为Borland曾违反此规则)。 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. 如果您定义模板函数一个转换单元(cpp文件),然后在另一个转换单元中以不同方式定义相同的函数,则只有两个版本中的一个将使其成为最终的可执行文件。 (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. (这实际上违反了“一个定义规则”,但适用于GCC,至少......不确定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. 如果您发现MSVC属实,我会敦促您更新文章(包括比较功能) - 如果MSVC是标准授权,就此而言。

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? C ++标准是否明确禁止此类使用?如果是这样,那么最近的编译器是否在这方面符合标准?

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. 我没有看到他违反ODR的任何地方,并且具有相同模板参数的函数模板的所有实例化都应该具有“相同的地址”(在某种意义上指向函数的指针应该都相等) - 如何实现这一点并不重要。 ISO C++03 14.5.5.1[temp.over.link] describes the rules in more detail. ISO C ++ 03 14.5.5.1 [temp.over.link]更详细地描述了规则。

So, a comparison could well be defined there in a conformant and portable way. 因此,可以以一致且可移植的方式在那里定义比较。

The so-called stub functions in Impossibly Fast C++ Delegates are static template member functions , which are basically template functions . Impossibly Fast C ++ Delegates中的所谓存根函数是静态模板成员函数 ,它们基本上是模板函数 The same applies for the improved variant Impossibly Fast C++ Delegates, Fixed . 这同样适用于改进的变体Impossibly Fast C ++ Delegates,Fixed

So the question boils down to this: 所以问题归结为:

Do instantiations of a template function (using the same template parameters and definition) in different translation units share the same function pointer address? 在不同的翻译单元中实例化模板函数(使用相同的模板参数和定义)是否共享相同的函数指针地址?

According to the C++ standard (ISO C++17, § 17.5.6.1), the answer is yes . 根据C ++标准(ISO C ++ 17,§17.5.6.1),答案是肯定的

As @Pavel said in his answer, the same applied for the ISO C++03 standard (ISO C++03, § 14.5.5.1). 正如@Pavel在他的回答中所说,同样适用于ISO C ++ 03标准(ISO C ++ 03,§14.5.5.1)。

In other words this approach is standard compliant and delegates are safely comparable - their data compare equal if and only if they are bound to the same function and (in case of member functions) the same object. 换句话说,这种方法是标准兼容的,并且委托是安全可比的 - 当且仅当它们绑定到相同的函数和(在成员函数的情况下)相同的对象时,它们的数据才相等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM