Are function attributes inherited?

I sent an email to the C++ committee, specifically the Core working group, and provided the above example.

CoryKramer

It is currently unclear from the standard if attributes applied to virtual functions are inherited.

Response:

They are not. For them to be inherited, the Standard would have to explicitly say so, and it does not.

CoryKramer:

[After providing above code example] In the above example, I would expect both lines calling foo() to emit a compiler warning. I would hope that the attribute applies to all derived functions for consistency.

Response:

That’s one perspective. Another is that, especially with covariant return types where the derived function returns a different type from that of the base function, it might very well be useful to make the base return type [[nodiscard]] but not the derived return type. There’s currently no way to mark the derived function as not-[[nodiscard]].

More generally, it seems reasonable to get a different set of attributes when calling a derived function from those you get when calling the base function. If you know you have a derived class object, you have more specific information and behavior than if all you know is that it’s a base class object, and attributes on member functions are part of that extra knowledge.

Reponses by Mike Miller of the C++ Core Working Group (3/30/2018).

Leave a Comment