简体   繁体   English

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

[英]Is noreturn part of the signature of a function?

[dcl.attr.noreturn] can be used to mark that a function doesn't return. [dcl.attr.noreturn]可用于标记函数不返回。

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

Is [[noreturn]] part to the identity/signature of a function? [[noreturn]]属于[[noreturn]]的标识/签名? can one detect that a function is noreturn at the time of compilation? 一个可以检测到功能noreturn在编译的时候?

For example, 例如,

static_assert(is_noreturn(f));

In case it is not, should I adopt a convention an define a tag struct? 如果不是,我应该采用约定来定义标签结构吗?

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

"signature" has a very precise definition. “签名”有一个非常精确的定义。 Well, several , depending on the kind of thing you are talking about: 好吧,有几个 ,取决于你所说的事情:

  • ⟨function⟩ name, parameter type list ([dcl.fct]), enclosing namespace (if any), and trailing requires-clause ([dcl.decl]) (if any) ⟨function⟩name,参数类型列表([dcl.fct]),封闭命名空间(如果有)和trailing requires-clause ([dcl.decl])(如果有的话)
  • ⟨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模板⟩名称,参数类型列表([dcl.fct]),封闭命名空间(如果有),返回类型, 模板头和尾随requires子句 ([dcl.decl])(如果有的话)
  • ⟨function template specialization⟩ signature of the template of which it is a specialization and its template arguments (whether explicitly specified or deduced) ⟨functiontemplatespecialization⟩模板的签名,它是一个特化及其模板参数(无论是明确指定还是推导)
  • ⟨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成员函数⟩名称,参数类型列表([dcl.fct]),函数为成员的类, cv-qualifiers (如果有), ref-qualifier (如果有)和trailing requires-clause ([ dcl.decl])(如果有的话)
  • ⟨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成员函数模板⟩名称,参数类型列表([dcl.fct]),函数为成员的类, cv-qualifiers (如果有), ref-qualifier (如果有),返回类型(如果有) , template-head和trailing requires-clause ([dcl.decl])(如果有的话)
  • ⟨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) ⟨class成员函数模板specialization⟩成员函数模板的签名,它是一个特化及其模板参数(无论是明确指定还是推导)

Attributes are not in any of them. 属性不属于任何属性。

[[noreturn]] is also not part of the type. [[noreturn]]也不属于该类型。 It appertains to the function, not its type. 它与功能有关,而不是它的类型。


can one detect that a function is noreturn at the time of compilation? 可以在编译时检测到函数是否正在编译?

No. The rule the committee established for attributes is that "compiling a valid program with all instances of a particular attribute ignored must result in a correct interpretation of the original program". 否。委员会为属性建立的规则是“编译一个有效程序,忽略特定属性的所有实例必须导致对原始程序的正确解释”。 That rule would not hold if you can programmatically detect an attribute's presence. 如果您可以以编程方式检测属性的存在,那么该规则就不会成立。


In case it is not, should I adopt a convention an[d] define a tag struct? 如果不是,我应该采用一个约定,[d]定义一个标签结构?

It's unclear what use such a tag would have. 目前还不清楚这样的标签会有什么用处。

If it were part of the type, a properly type checking compiler would not accept eg, something like: 如果它是类型的一部分,正确的类型检查编译器将不会接受例如:

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

The above compiles without error. 以上编译没有错误。 [[noreturn]] is not part of the type. [[noreturn]]不属于该类型。 (Incidentally, neither is _Noreturn in C11, where it is placed syntactically into the same category as inline ). (顺便说一句,在C11中,它都不是_Noreturn ,它在语法上被放置在与inline相同的类别中)。

As for detecting it, I didn't find any mechanism for it in the C++11 standard draft. 至于检测它,我在C ++ 11标准草案中找不到任何机制。 A convention such as the one you proposed could allow you to detect it, but you'd be limited to functions that follow such a convention. 像你提议的那样的约定可以允许你检测它,但你只能遵循遵循这种约定的函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM