[cfe-dev] [Warnings] final class with virtual functions

Aaron Ballman via cfe-dev cfe-dev at lists.llvm.org
Thu Dec 22 07:52:55 PST 2016


On Thu, Dec 22, 2016 at 10:49 AM, Malcolm Parsons
<malcolm.parsons at gmail.com> wrote:
> On 22 Dec 2016 2:33 p.m., "Aaron Ballman via cfe-dev"
> <cfe-dev at lists.llvm.org> wrote:
>
> On Wed, Dec 21, 2016 at 8:24 PM, Piotr Padlewski
> <piotr.padlewski at gmail.com> wrote:
>> Hi,
>> I was wondering if we should warn about cases like this:
>>
>> struct A final {
>>
>>    virtual int foo () {return 42;}
>>
>>    // virtual doesn't make any sense because it is final base class
>>
>> };
>>
>>
>> or
>>
>>
>> struct A {
>>
>>   virtual int foo () final {return 42;}
>>
>>   // the same thing
>>
>> };
>>
>>
>> So in both cases it sounds like a bug, specially in the first case.
>
> I don't think it sounds like a bug (at least, nothing we'd want to
> diagnose in the frontend).
>
>
> I think it's a bug, but I also think it's unlikely to happen.
>
> Prior to the override keyword, some coding style guidelines
> recommended you write virtual on all virtual functions (instead of
> just the first declaration of such a function in a base class) to make
> it clear that the function is one that can be overridden.
>
>
> The warning is about declaring a virtual method that cannot be overridden.
> It wouldn't trigger for methods that are overriding, whether the override
> keyword is used it not.

Ah, I missed the fact that there was no base class in the examples and
was thinking of this situation:

struct B {
  virtual void f() { /* ... */ }
};

struct B final : D {
  virtual void f() { /* ... */ }
};

So long as we don't warn on this, I'm fine with it being a frontend diagnostic.

~Aaron



More information about the cfe-dev mailing list