Re: Delegates
Martin Vejn1r wrote:
I've just read a post in comp.lang.c++.moderated submitted by Niek
Sanders. There is a link to a very interesting article:
[http://www.codeproject.com/cpp/FastDelegate.asp]
As explained in the article, native support for delegates (e.g. pointers
to members bound to an object) would boost performance of code which
uses delegates heavily.
Is there any reason why C++ doesn't support them? Did C++ committee
discuss this subject?
In Standard C++, a way to implement "delegates" is illustrated by
this:
struct BaseDelegateSetI
{
virtual void setI(int i) = 0;
};
template<class C>
class DelegateSetI
{
private:
C &c:
public:
DelegateSetI(C &cc) : c(cc) { }
void setI(int i) { c.setI(i); }
};
So, in deciding whether to add native delegates to the
Standard, the rant against member function pointers
is perhaps a red herring.
This example would seem to indicate that delegates can
be thought of as a shorthand for abstract base classes
with a single member function. It's true that native
delegates would be little more optimal in both space
and time when evaluated at the level of a single function
call. But what if the object calling member functions
of the instance of C had to call several different members
of C? Then the use of delegates would result in
unecessary redundant references to C (in each
delegate).
So I think it's still an open question whether
using builtin delegates would really result in
optimizing the object code generated from
"typical" C++ code.
If it's truely a common case for classes to
have a single virtual function, it might be
possible to optimize for that case
by not having a vtable, and putting the
function pointer in the object in place of
the vpointer.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]