This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: RFC: New C++ Attribute: final
Alexandre Oliva <aoliva@redhat.com> writes:
| On Mar 1, 2004, Gabriel Dos Reis <gdr@integrable-solutions.net> wrote:
|
| > Jeff Sturm <jsturm@one-point.com> writes:
| > | > From a programmer's perspective, why is it desirable to remove the
| > | > virtuality?
| > |
| > | For C++, I can't think of a good reason besides performance. In Java, I
| > | think 'final' is essential to the security framework.
|
| > There is no doubt that people will construct examples where they
| > think they need final for C++.
|
| One of them will probably have to do with passing pointers to member
| functions to STL algorithms.
Taking a pointer-to-member is a very *general* mechanism, that comes
with its price. And in pratice, it is far more efficient (whether the
member function is virtual or not is immaterial) just to use custom
function objects that directly call the function: You get far *better*
efficiency.
| There's no way in C++ that I know to
| create a pointer to member that doesn't undergo virtual method
| dispatch if the named method turns out to be virtual. Sure you can
When you create a pointer to member function, the member
function does not need to be virtual, but still you get *much more*
inefficiency (and that is worse if your happens to be GCC). And final
does not help here. There are well-knwon techniques to write clearer
and more efficient codes: Use function objects that directly call the
function. You may even get inlining if the function were inline.
That is what the STL has been doing :-)
| introduce a separate private method and not only call it from the
| virtual method, but also use it when you need a non-dynamic pointer to
| method, but you have to hope nobody ever decides to introduce a
| virtual method in a base class using the same name, to enable a
| derived class to preempt your wish for a non-virtual pointer to
| method.
People keep damaging codes -- in the name of whatever (usually,
generality). I believe people who damage their codes get what they
deserve :-)
| Not that I personally find the need for non-virtual pointers to
| members in good designs. It's just that, if final is added to C++,
| people will probably want to annotate expressions that take the
| address of member functions, and from that even function calls as
| final! The horror! :-) :-)
With the help of final or not, people are going to damage their codes
anyway :-)
-- Gaby