gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 16:33:59 24 4
gpt4 key购买 nike

[dcl.attr.noreturn]可用于标记函数不返回。

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

[[noreturn]] 是函数标识/签名的一部分吗?可以在编译时检测到一个函数是 noreturn 吗?

例如,

static_assert(is_noreturn(f));

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

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

最佳答案

“签名”有一个非常精确的定义。嗯,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?

没有。委员会为属性制定的规则是“编译一个忽略特定属性的所有实例的有效程序必须导致对原始程序的正确解释”。如果您可以编程方式检测属性的存在,则该规则将不成立。


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

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

关于c++ - noreturn 是函数签名的一部分吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51268755/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com