Reputation: 54737
I want to declare a friend function to a class as [[noreturn]]
but I cannot figure out the correct syntax.
struct a
{
[[noreturn]] friend int b();
};
[[noreturn]] int b(){ throw 42; }
This fails to compile on both gcc and clang:
GCC error message:
warning: attribute ignored [-Wattributes]
X | [[noreturn]] friend int b();
| ^
note: an attribute that appertains to a friend declaration that is not a definition is ignored
error: function 'int b()' declared '[[noreturn]]' but its first declaration was not
Y | [[noreturn]] int b(){ throw 42; }
| ^
<source>:5:29: note: previous declaration of 'int b()'
X | [[noreturn]] friend int b();
| ^
Clang error message:
error: an attribute list cannot appear here
X | [[noreturn]] friend int b();
| ^~~~~~~~~~~~
error: 'noreturn' attribute does not appear on the first declaration
Y | [[noreturn]] int b(){ throw 42; }
| ^
note: previous declaration is here
X | [[noreturn]] friend int b();
| ^
Upvotes: 1
Views: 69
Reputation: 54737
Attribute specifiers are not allowed on friend declarations that are not also definitions.
As you probably don't want to move the function definition inside the class body in this case, forward declare the function with the correct attributes once before the class definition:
[[noreturn]] int b();
struct a
{
friend int b();
};
[[noreturn]] int b(){ throw 42; }
Upvotes: 3