gpt4 book ai didi

c++ - 警告 C5240 : 'nodiscard' : attribute is ignored in this syntactic position

转载 作者:行者123 更新时间:2023-12-03 17:17:47 28 4
gpt4 key购买 nike

最近发布了 Visual Studio 2019 的 16.9.5 版本。它显然引入了新的警告:

[[nodiscard]] __declspec(dllexport) bool foo(); //ok
__declspec(dllexport) [[nodiscard]] bool bar(); // warning C5240: 'nodiscard': attribute is ignored in this syntactic position
其实我以为 nodiscard 和 dllexport 都是可以按任何顺序出现的属性,或者不是?

最佳答案

我今天也收到了这个警告,所以决定调查一下。这需要看一下标准,并将不同的部分放在一起。
根据 [dcl.fct.def.general] ,函数定义为:

function-definition:
attribute-specifier-seq_opt decl-specifier-seq_opt declarator virt-specifier-seq_opt function-body
这很重要的原因是它指定可选的 decl-specifier-seq 出现在 attribute-specifier-seq(包括 [[nodiscard]] )之后。
现在,根据 [dcl.spec.general] decl-specifier-seq 定义为:
decl-specifier-seq:
decl-specifier attribute-specifier-seq_opt
decl-specifier decl-specifier-seq
根据微软的文档 __declspec ,它被定义为 decl-specifier ;因此, __declspec(dllexport) 必须在 [[nodiscard]] 属性之后。
笔记:

The optional attribute-specifier-seq in a decl-specifier-seq appertains to the type determined by the precedingdecl-specifiers


因此, [[nodiscard]] 之后的 __declspec(dllexport) 仅适用于 __declspec(dllexport)

关于c++ - 警告 C5240 : 'nodiscard' : attribute is ignored in this syntactic position,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67502217/

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