tags:

views:

810

answers:

1

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 as it best suits my needs. 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 conformant, 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 basically wondering if there's any merit to the above comment and whether this specific implementation of fast C++ delegates can indeed be changed to support comparisons. Does the C++ standard specifically prohibit such usage and even if that's so, do any of the recent compilers are actually standard-compliant in that regard?

Thanks.

+6  A: 

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.

Pavel Minaev