Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does attribute specifier sequence inherit?

Tags:

Look at this snippet:

struct [[nodiscard]] Result {
};


struct DiscardableResult: Result {
};

Does DiscardableResult have the [[nodiscard]] attribute? If yes, is it possible to remove it somehow?

like image 770
geza Avatar asked Nov 03 '17 21:11

geza


People also ask

What's the difference between inherited and derived class access specifiers?

So what’s the difference between these? In a nutshell, when members are inherited, the access specifier for an inherited member may be changed (in the derived class only) depending on the type of inheritance used. Put another way, members that were public or protected in the base class may change access specifiers in the derived class.

How many types of inheritance are there in C++?

If you do not choose an inheritance type, C++ defaults to private inheritance (just like members default to private access if you do not specify otherwise). That gives us 9 combinations: 3 member access specifiers (public, private, and protected), and 3 inheritance types (public, private, and protected).

How do I inherit a class from another class in C++?

To do so, simply specify which type of access you want when choosing the class to inherit from: If you do not choose an inheritance type, C++ defaults to private inheritance (just like members default to private access if you do not specify otherwise).

What are the different types of access specifiers available in C++?

You learned from the Access Specifiers chapter that there are three specifiers available in C++. Until now, we have only used public (members of a class are accessible from outside the class) and private (members can only be accessed within the class).


1 Answers

[dcl.attr.nodiscard]/2 says:

A nodiscard call is a function call expression that calls a function previously declared nodiscard, or whose return type is a possibly cv-qualified class or enumeration type marked nodiscard.

The return type of the function is DiscardableResult. This type is not marked nodiscard, as defined in [dcl.attr.grammar]/5:

Each attribute-specifier-seq is said to appertain to some entity or statement, identified by the syntactic context where it appears (Clause 9, Clause 10, Clause 11). If an attribute-specifier-seq that appertains to some entity or statement contains an attribute or alignment-specifier that is not allowed to apply to that entity or statement, the program is ill-formed. If an attribute-specifier-seq appertains to a friend declaration (14.3), that declaration shall be a definition. No attribute-specifier-seq shall appertain to an explicit instantiation (17.7.2).

Emphasis added.

There is no attribute in the "syntactic context" of DiscardableResult. Therefore, no attribute "appertains" to this entity.

Attributes are not inherited.

like image 96
Nicol Bolas Avatar answered Oct 11 '22 19:10

Nicol Bolas