[cfe-dev] Should adding 'final' to a C++11 class change linking behavior?

Sebastian Redl sebastian.redl at getdesigned.at
Mon Nov 21 10:39:53 PST 2011


On 21.11.2011, at 19:13, Jeff Walden wrote:

> 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?

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

class A {
  virtual void foo();
};

A *a;
a->foo();

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.

Sebastian



More information about the cfe-dev mailing list