vendredi 15 novembre 2019

g++ [[noreturn]] on a virtual method

I've run into some trouble trying to clean up g++ compiler warnings.

Say I have this class:

class A
{
public:
    [[noreturn]] virtual void will_throw() { throw 0; }
};

And inside a non-void function I invoke will_throw without returning.

If I do this by value, i.e.:

int g()
{
    A a;
    a.will_throw();
}

then I get no -Wreturn-type warnings.

If I do it by pointer:

int g()
{
    A a;
    A* aptr = &a;
    aptr->will_throw();
}

Then I get "warning: no return statement in function returning non-void [-Wreturn-type]"

If I remove virtual from the declaration of A::will_throw then calling it on a pointer also produces no warnings. Calling the method on a reference seems to produce a warning if the method is pure-virtual, but not otherwise.

I wasn't able to find anything saying this is how it's supposed to work, and none of these cases produce warnings in Clang. Is this a bug in GCC?

Aucun commentaire:

Enregistrer un commentaire