Should adding 'final' to a C++11 class change linking behavior?

Should adding 'final' to a class change linking behavior?

I have a class which I'd like to mark as final. Nothing inherits from it, so it should be fine to mark it as such. However, when I do so, I get a bunch of undefined-reference warnings linking code that uses pointers to that class. (The class itself is never instantiated.) My understanding was that 'final' solely prevented inheritance/overriding, and I'd think that any linking failure like this would be a bug. Is that understanding correct?

Producing a reduced testcase from this situation may be tricky, so I'd like to know if I'm missing something obvious before trying to make one.


Yes. Just a guess, but the linking errors are probably because when you have code like this:

class A {
  virtual void foo();

A *a;

the compiler has to generate a virtual call, but if you mark A as final, it will generate a direct call, since 'a' can only point to an A and not to a derived class. Could be that this optimization doesn't correctly emit vtables then.


clang will optimize calls to a virtual function in a final class to
non-virtual calls. See canDevirtualizeMemberFunctionCall in