How does the compiler benefit from C++’s new final keyword?

I can think of one scenario where it might be helpful to the compiler from an optimisation perspective. I’m not sure if it’s worth the effort to compiler implementers, but it’s theoretically possible at least.

With virtual call dispatch on a derived, final type you can be sure that there is nothing else deriving from that type. This means that (at least in theory) the final keyword would make it possible to correctly resolve some virtual calls at compile time, which would make a number of optimisations possible that were otherwise impossible on virtual calls.

For example, if you have delete most_derived_ptr, where most_derived_ptr is a pointer to a derived, final type then it’s possible for the compiler to simplify calls to the virtual destructor.

Likewise for calls to virtual member functions on references/pointers to the most derived type.

I’d be very surprised if any compilers did this today, but it seems like the kind of thing that might be implemented over the next decade or so.

There might also be some millage in being able to infer that (in the absence of friends) things marked protected in a final class also effectively become private.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)