gpt4 book ai didi

c++ - 属性说明符序列是否继承?

转载 作者:IT老高 更新时间:2023-10-28 21:57:46 25 4
gpt4 key购买 nike

看看这个片段:

struct [[nodiscard]] Result {
};


struct DiscardableResult: Result {
};

DiscardableResult 是否有 [[nodiscard]] 属性?如果是,是否有可能以某种方式将其删除?

最佳答案

[dcl.attr.nodiscard]/2 说:

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。这种类型没有标记为nodiscard,定义在[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).

添加了重点。

DiscardableResult 的“句法上下文”中没有属性。因此,没有属性“属于”该实体。

属性不会被继承。

关于c++ - 属性说明符序列是否继承?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47104799/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com