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?
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.
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).
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).
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).
[dcl.attr.nodiscard]/2 says:
A
nodiscard
call is a function call expression that calls a function previously declarednodiscard
, or whose return type is a possibly cv-qualified class or enumeration type markednodiscard
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With