RFC: New C++ Attribute: final
Joe Buck
Joe.Buck@synopsys.COM
Mon Mar 1 18:45:00 GMT 2004
On Mon, Mar 01, 2004 at 10:20:17AM +0000, Nathan Sidwell wrote:
> Kevin Atkinson wrote:
> > Well yes I got the name from Java. But that doesn't mean that it can't be
> > useful is C++ also. Furthermore this attribute is mainly an
> > optimization so your program will still be correct without it.
>
> In Java, the point of 'final' is to say 'not virtual', because all
> non static member functions are implicitly virtual. In C++ there's no need.
This is incorrect. "final" permits a performance win in Java that C++
does not have, as it permits the compiler to eliminate many virtual calls.
The key is that Base::func can be labeled virtual while Derived::func
can be labeled final. You can't do that in C++.
I first saw a proposal for "final" in C++ in the context of a paper on
the implementation of matrix classes of various forms (sparse matrix,
symmetrix matrix, general dense matrix, etc) in 1992. The key observation
is that if we make the base element access method virtual, but then make
the derived element access methods final, we can write algorithms for the
derived classes in a natural way and avoid virtual calls. In the absence
of "final", I've often been reduced to having two forms of calls, a
virtual form and a nonvirtual form, which can be error-prone.
More information about the Gcc
mailing list