Does attribute specifier sequence inherit?

烂漫一生 提交于 2019-12-17 18:35:23

问题


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?


回答1:


[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.



来源:https://stackoverflow.com/questions/47104799/does-attribute-specifier-sequence-inherit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!