[cxx-abi-dev] Key functions and templates

Mark Mitchell mark at codesourcery.com
Mon Feb 22 23:19:10 UTC 2010


Lawrence Crowl wrote:

>>> By the letter of the ABI they are, since each of them is the
>>> first non-inline non-pure virtual function of its class.
>> Yes, this has bothered me for a while.  They are key functions,
>> but is this useful?
> 
> I don't think so.  The point of key functions is to prevent redundant
> virtual table generation by making their generation a side effect
> of something that is only done once.  That property doesn't exist
> with templates.

Well, yes and no.  You won't get a linker error, and you won't get
wasted space in your final executable, but, without key functions, you
will get bigger object files, slower compilers, and -- on a SVR4-ish
system -- duplicate definitions in multiple shared objects.  Those
duplicate definitions will be collapsed by the dynamic linker, but if
they don't need to be there, you're still wasting space.

>   A translation unit declares an "extern template" specialization
>   of G_key_defined<int>::f().  The compiler infers that the virtual
>   table need not be generated even though the translation unit
>   otherwise instantiates G_key_defined<int>.  The specialization of
>   f() is not subsequently used, and so the vtable will fail to exist.

Is this a valid program?  If the vtable was created at the point it is
referenced, it would reference the virtual function "f", not otherwise
used, by hypothesis.  Are we allowed to instantiate the function at this
point?  (This is not a rhetorical question; I don't recall what the
standard says about the point of instantiation in this case.)

In any cases, Alasdair is saying that G++ generates too many vtables,
not too few; this seems potentially non-optimal, but I don't see how it
breaks anything.

-- 
Mark Mitchell
CodeSourcery
mark at codesourcery.com
(650) 331-3385 x713



More information about the cxx-abi-dev mailing list