属性说明符序列是否继承? [英] Does attribute specifier sequence inherit?

查看:103
本文介绍了属性说明符序列是否继承?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看看这个片段:

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 declared nodiscard, or whose return type is a possibly cv-qualified class or enumeration type marked nodiscard.

该函数的返回类型为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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆