c++ - 警告 C5240:“nodiscard”:在此句法位置忽略属性
问题描述
最近发布了 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]
,adecl-specifier-seq
定义为:
decl-specifier-seq:
decl-specifier attribute-specifier-seq_opt
decl-specifier decl-specifier-seq
根据微软的文档__declspec
,它被定义为decl-specifier
; 因此,__declspec(dllexport)
必须在[[nodiscard]]
属性之后。
笔记:
decl-specifier-seq 中的可选属性说明符序列属于由前面的 decl 说明符确定的类型
因此,[[nodiscard]]
后面的__declspec(dllexport)
仅适用于__declspec(dllexport)
推荐阅读
- symfony - symfony 服务器和 bin/console 服务器之间的区别?
- c# - 如何修复“java.lang.NoClassDefFoundError:解析失败:Landroid/support/v4/content/ContextCompat”
- mercurial - hg 标签的返回值是多少 --template "{date|isodate}\n"
- android - 即使在观察者被移除后,实时数据观察者也会被调用
- python - 对python中的部分数据框做一些处理
- firebase - 如何在 Firebase Functions shell 中运行 Pub-Sub 函数
- python - 如何在python中正确导入包?
- python - 如何在熊猫中旋转这个数据框
- python - 无法在 Python 中发出 HTTP 请求
- android - IndexOutOfBoundsException: Invalid index 0, size is 0. 我该如何解决这个问题