`friend` 成员函数和属性 - gcc vs clang
`friend` member functions and attributes - gcc vs clang
以下代码片段:
struct a
{
[[nodiscard]] friend int b();
};
使用 -std=c++17
在 clang++ (trunk 342102)
上编译时产生此错误:
<source>:3:5: error: an attribute list cannot appear here
[[nodiscard]] friend int b();
^~~~~~~~~~~~~
删除 friend
或向 b
添加正文可以防止错误。
g++ (trunk)
编译代码就好了。
godbolt 实例:https://gcc.godbolt.org/z/ttTDuZ
这是clang++
错误吗?还是标准中有一些规则导致此代码格式错误?
如果clang++
是正确的,将friend
成员函数标记为[[nodiscard]]
的正确方法是什么?
Each attribute-specifier-seq is said to appertain to some entity or statement, identified by the syntactic context where it appears ([stmt.stmt], [dcl.dcl], [dcl.decl]). 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, that declaration shall be a definition. No attribute-specifier-seq shall appertain to an explicit instantiation.
强调我的
所以,clang 就在这里。如果有属性,函数如果是友元函数就必须有定义。
以下代码片段:
struct a
{
[[nodiscard]] friend int b();
};
使用 -std=c++17
在 clang++ (trunk 342102)
上编译时产生此错误:
<source>:3:5: error: an attribute list cannot appear here
[[nodiscard]] friend int b();
^~~~~~~~~~~~~
删除 friend
或向 b
添加正文可以防止错误。
g++ (trunk)
编译代码就好了。
godbolt 实例:https://gcc.godbolt.org/z/ttTDuZ
这是
clang++
错误吗?还是标准中有一些规则导致此代码格式错误?如果
clang++
是正确的,将friend
成员函数标记为[[nodiscard]]
的正确方法是什么?
Each attribute-specifier-seq is said to appertain to some entity or statement, identified by the syntactic context where it appears ([stmt.stmt], [dcl.dcl], [dcl.decl]). 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, that declaration shall be a definition. No attribute-specifier-seq shall appertain to an explicit instantiation.
强调我的
所以,clang 就在这里。如果有属性,函数如果是友元函数就必须有定义。