[cfe-dev] Following up on C++11 final and undefined-reference linker errors
John McCall
rjmccall at apple.com
Tue Nov 29 20:04:33 PST 2011
On Nov 29, 2011, at 7:35 PM, Eli Friedman wrote:
> On Tue, Nov 29, 2011 at 7:20 PM, John McCall <rjmccall at apple.com> wrote:
>> On Nov 29, 2011, at 5:34 PM, Jeff Walden wrote:
>>> http://lists.cs.uiuc.edu/pipermail/cfe-dev/2011-November/018779.html
>>>
>>> A week ago I asked if adding |final| to a class causing linker errors could possibly not be a compiler bug; replies suggested it likely would be one. Having reduced it to the code at the end of this mail, I'm pretty sure it's not one. [expr.reinterpret.cast]p7 says reinterpret_cast<> on a class with virtual functions, or to a class which is not an actual type of the instance being cast, has undefined behavior. If I read this right, failure to link is permissible behavior. (GCC 4.7 also produces a linking error.)
>>
>> Only if the undefined behavior is provably reached. Undefined behavior only taints execution paths which reach the undefined behavior.
>>
>> Your program, however, is outright ill-formed, with no diagnostic required, because it contains the declaration of a virtual function (Wrapper::hidden()), but nowhere in the program is there a definition for it. All virtual functions are considered "used" and therefore must be defined, even if their classes are never even mentioned elsewhere in the program.
>
> hidden() isn't really relevant here: we end up with the same behavior
> even if you completely remove it from the testcase.
You're right; I've misread the testcase.
I am still totally comfortable with unconditionally rejecting the definition of a final abstract class, however. Similarly, we should reject final pure virtual functions, although the error there is much more obvious and thus less likely to occur in practice.
John.
More information about the cfe-dev
mailing list