Key functions and templates

Alasdair Grant Alasdair.Grant at arm.com
Mon Feb 22 17:03:16 UTC 2010


Hi,

Given the following:

  /* Key function, defined */
  struct C_key_defined { virtual int f(); };
  int C_key_defined::f() { return 1; }
  int f_key_defined(void) { C_key_defined c; return c.f(); }

  /* Key function, not defined */
  struct C_key_nodef { virtual int f(); };
  int f_key_nodef(void) { C_key_nodef c; return c.f(); }

  /* No key function */
  struct C_no_key { inline int f() { return 1; } };
  int f_no_key(void) { C_no_key c; return c.f(); }

  /* Key function(?), defined */
  template<typename T> struct G_key_defined { virtual int f(); };
  template<typename T> int G_key_defined<T>::f() { return 1; }
  int g_key_defined(void) { G_key_defined<int> c; return c.f(); }

  /* Key function(?), not defined */
  template<typename T> struct G_key_nodef { virtual int f(); };
  int g_key_nodef(void) { G_key_nodef<int> c; return c.f(); }

  /* No key function */
  template<typename T> struct G_no_key { inline int f() { return 1; } };
  int g_no_key(void) { G_no_key<int> c; return c.f(); }

are G_key_defined<int>::f() and G_key_nodef<int>::f() key functions?

By the letter of the ABI they are, since each of them is the first
non-inline non-pure virtual function of its class.

But surely the spirit of the ABI is to use the key function as a
'key' to identify a unique translation unit - but these functions
may be multiply defined.  So if the vtables for G_key_defined<int>
and G_key_nodef<int> are defined at all, they must be defined with
vague linkage.

Implementations (at least of the ARM variant of the ABI) seem to
differ about whether the code above should define the vtable for
G_key_nodef<int>.  GCC 4.4 for ARM does define the vtable - which
suggests it is not treating G_key_nodef<int>::f() as a key function
(if it did, it could rely on some other unit to be defining f(),
and hence the vtable).

I can't see any discussion of this point in the generic ABI, but it
might be worth a clarification.

Thanks,

Al Grant

-- IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium.  Thank you.



More information about the cxx-abi-dev mailing list