属性说明符序列是否继承? [英] Does attribute specifier sequence inherit?
问题描述
看看这个片段:
struct [[nodiscard]] Result {
};
struct DiscardableResult: Result {
};
DiscardableResult
是否具有[[nodiscard]]
属性?如果是,是否可以通过某种方式将其删除?
Does DiscardableResult
have the [[nodiscard]]
attribute? If yes, is it possible to remove it somehow?
推荐答案
[dcl.attr.nodiscard]/2说:
[dcl.attr.nodiscard]/2 says:
nodiscard
调用是一个函数调用表达式,该函数调用先前声明过的nodiscard
或返回类型为可能是cv限定的类或标记为nodiscard
的枚举类型的函数.
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
.
该函数的返回类型为DiscardableResult
. [dcl.attr.grammar]/5中定义的该类型未标记为nodiscard
:
The return type of the function is DiscardableResult
. This type is not marked nodiscard
, as defined in [dcl.attr.grammar]/5:
据说每个attribute-specifier-seq都属于某个实体或语句,由语法上下文出现在其中所标识(第9条,第10条,第11条).如果属于某个实体或语句的attribute-specifier-seq包含不允许应用于该实体或语句的属性或比对说明符,则程序格式错误.如果一个属性说明符序列属于一个朋友声明(14.3),则该声明应为定义.不能将attribute-specifier-seq属于显式实例化(17.7.2).
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).
添加了重点.
DiscardableResult
的语法上下文"中没有属性.因此,没有属性属于"该实体.
There is no attribute in the "syntactic context" of DiscardableResult
. Therefore, no attribute "appertains" to this entity.
属性不会被继承.
这篇关于属性说明符序列是否继承?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!