什么是不返回的意义呢

什么是不返回的意义呢

本文介绍了什么是不返回的意义呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

提供了以下例子:

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

但我不明白什么是 [noreturn的] ,因为函数的返回类型已经是无效

but I do not understand what is the point of [[noreturn]], because the return type of the function is already void.

那么,什么是不返回的属性点?它应该如何使用?

So, what is the point of the noreturn attribute? How is it supposed to be used?

推荐答案

noreturn属性应该用于那些不返回给调用者的功能。这并不意味着无效函数(不要返回给调用者 - 他们只是不返​​回值),但功能,其中控制流不会返回给调用函数的函数完成后(即退出应用程序如功能,永远循环下去或抛出异常如在你的例子)。

The noreturn attribute is supposed to be used for functions that don't return to the caller. That doesn't mean void functions (which do return to the caller - they just don't return a value), but functions where the control flow will not return to the calling function after the function finishes (e.g. functions that exit the application, loop forever or throw exceptions as in your example).

这可以通过编译器可以用来做一些优化,并产生更好的警告。例如,如果˚F的noreturn属性,编译器会警告你克()虽然死了,code当你写 F();克(); 。同样,编译器将不知道要提醒你F()

This can be used by compilers to make some optimizations and generate better warnings. For example if f has the noreturn attribute, the compiler could warn you about g() being dead code when you write f(); g();. Similarly the compiler will know not to warn you about missing return statements after calls to f().

这篇关于什么是不返回的意义呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 13:29