mercredi 27 juillet 2016

error: function declared 'noreturn' should not return

At my workplace, we have a different internal name for noreturn attribute. Suppose it is INTERNAL_DONT_RETURN

I am writing a member function of a class where I do something like

INTERNAL_DONT_RETURN void foo() const
{
    if(!*this)
    {
       throw CoolException();
    }
    m_call_throw();
}

This m_call_throw() is a private class member std::function<void()>m_call_throw which gets populated in the class's constructor as a lambda. This lambda does nothing but

m_call_throw([uncoolID]() { throw UncoolException(uncoolID); })

Now both, gcc-4.9.3 and clang gives me following warning

error: function declared 'noreturn' should not return [-Werror,-Winvalid-noreturn] } ^

I have already consulted this and this question but none of them explained the cause of above warning.

1) Is compiler implicitly adding return as explained here ?

2) Even when I am throwing exceptions why does compiler believe that my function will return?

3) noreturn attribute mentions that

The noreturn keyword does not affect the exceptional path when that applies: a noreturn-marked function may still return to the caller by throwing an exception or calling longjmp.

Is this relevant to my problem?

Aucun commentaire:

Enregistrer un commentaire