6

I think it's not really a duplicate of Are function attributes inherited?, because I'm wondering about classes, not member functions :

struct [[nodiscard]] error {};
struct critical_error : error {};

critical_error foo();

int main() {
   foo(); // no warning.
}

It seems that the [[nodiscard]] attribute is not inherited here. Is it the same for all type-attributes?

Not a real meerkat
  • 5,604
  • 1
  • 24
  • 55
dvr33
  • 145
  • 1
  • 3
  • 11

1 Answers1

3

They aren't, as you asserted yourself. The standard is explicit in what exactly is inherited from a base class to a derived one:

10.6 Derived classes [class.derived]

2 [...] Unless redeclared in the derived class, members of a base class are also considered to be members of the derived class. Members of a base class other than constructors are said to be inherited by the derived class. Constructors of a base class can also be inherited as described in [namespace.udecl]. Inherited members can be referred to in expressions in the same manner as other members of the derived class, unless their names are hidden or ambiguous ([class.member.lookup]).

For the sake of completeness: There is also no wording about inheritance in the specific section about attributes.

Basically: an attribute is not a member of the class or a constructor, so it can't be inherited.

Not a real meerkat
  • 5,604
  • 1
  • 24
  • 55