noreturn 是函数签名的一部分吗?

Is noreturn part of the signature of a function?

[dcl.attr.noreturn] 可用于标记函数不 return.

[[ noreturn ]] void f() {
    throw "error";
}

[[noreturn]] 是函数 identity/signature 的一部分吗?可以在编译时检测到一个函数是 noreturn 吗?

例如,

static_assert(is_noreturn(f));

如果不是,我是否应该采用约定来定义标签结构?

struct noreturn_{noreturn_()=delete;};
...
[[noreturn]] noreturn_ f(){throw "error";}

如果它是类型的一部分,正确的类型检查编译器将不会接受 例如:

[[noreturn]] int f(void);
int (*fp)(void) = f;

以上编译没有错误。 [[noreturn]] 不是类型的一部分。 (顺便说一句,_Noreturn 在 C11 中也不是,它在句法上与 inline 属于同一类别)。

至于检测,我在C++11标准草案中没有找到任何检测机制。像您提出的那样的约定可以让您检测到它,但您将仅限于遵循此类约定的函数。

"signature" 有一个非常精确的定义。好吧,several,取决于你说的是什么类型的东西:

  • ⟨function⟩ name, parameter type list ([dcl.fct]), enclosing namespace (if any), and trailing requires-clause ([dcl.decl]) (if any)
  • ⟨function template⟩ name, parameter type list ([dcl.fct]), enclosing namespace (if any), return type, template-head, and trailing requires-clause ([dcl.decl]) (if any)
  • ⟨function template specialization⟩ signature of the template of which it is a specialization and its template arguments (whether explicitly specified or deduced)
  • ⟨class member function⟩ name, parameter type list ([dcl.fct]), class of which the function is a member, cv-qualifiers (if any), ref-qualifier (if any), and trailing requires-clause ([dcl.decl]) (if any)
  • ⟨class member function template⟩ name, parameter type list ([dcl.fct]), class of which the function is a member, cv-qualifiers (if any), ref-qualifier (if any), return type (if any), template-head, and trailing requires-clause ([dcl.decl]) (if any)
  • ⟨class member function template specialization⟩ signature of the member function template of which it is a specialization and its template arguments (whether explicitly specified or deduced)

其中任何一个都没有属性。

[[noreturn]] 也不是类型的一部分。它属于函数,而不是它的类型。


can one detect that a function is noreturn at the time of compilation?

没有。委员会为属性制定的规则是"compiling a valid program with all instances of a particular attribute ignored must result in a correct interpretation of the original program"。如果您可以编程方式检测属性的存在,则该规则将不成立。


In case it is not, should I adopt a convention an[d] define a tag struct?

不清楚这样的标签有什么用。