gnz*_*lbg 17 c++ lambda attributes c++11 noreturn
如何operator()将lambda声明为noreturn?
Ideone接受以下代码:
#include <cstdlib>
int main() {
[]() [[noreturn]] { std::exit(1); }();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Clang 3.5拒绝它:
error: 'noreturn' attribute cannot be applied to types
Run Code Online (Sandbox Code Playgroud)
你可以在godbolt中试试:http://goo.gl/vsuCsF
哪一个是对的?
更新:相关的标准部分似乎是5.1.2.5,7.6.3,7.6.4,但在阅读之后,我仍然不是100%明确的(i)正确的行为是什么,(ii)如何标记lambda的operator()为noreturn.
T.C*_*.C. 23
铿锵是对的.属性可以属于声明的函数或其类型; 两者是不同的.[[noreturn]]必须附属于功能本身.差异可见于
// [[noreturn]] appertains to the entity that's being declared
void f [[noreturn]] (); // §8.3 [dcl.meaning]/p1:
// The optional attribute-specifier-seq following a
// declarator-id appertains to the entity that is declared."
[[noreturn]] void h (); // §7 [dcl.dcl]/p2:
// "The attribute-specifier-seq in a simple-declaration
// appertains to each of the entities declared by
// the declarators of the init-declarator-list."
// ill-formed - [[noreturn]] appertains to the type (§8.3.5 [dcl.fct]/p1:
// "The optional attribute-specifier-seq appertains to the function type.")
void g () [[noreturn]] {}
Run Code Online (Sandbox Code Playgroud)
事实上,如果你用g ++编译它就会告诉你
warning: attribute ignored [-Wattributes]
void g () [[noreturn]] {}
^
note: an attribute that appertains to a type-specifier is ignored
Run Code Online (Sandbox Code Playgroud)
请注意,它不会发出g()实际返回的警告.
由于一个" 属性说明符-SEQ的λ-声明符 appertains到该类型相应的函数调用操作或操作员的模板"(§5.1.2[expr.prim.lambda]/P5),而不是该操作员/操作模板本身,你不能[[noreturn]]在那里使用.更一般地说,该语言无法将属性应用于operator ()lambda本身.