如何使 Qt Creator 在没有 return 语句时显示错误

发布于 2024-11-28 11:39:48 字数 846 浏览 1 评论 0原文

事实证明,如果您在非 void 函数中没有 return 语句,g++ 编译器(默认在 Qt Creator 中使用)只会给出警告,即:

int* create_array(int n)
{
    int* a = new int[n];
}

编译正常。

这种行为受到 g++ 本身无数错误报告的影响,但看起来开发人员认为这种行为符合 C++ 标准(这是有争议的,因为这部分有点令人困惑),如 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43943

Flowing off the end of a function is equivalent to a return with no value; 
this results in undefined behavior in a value-returning function.

然而,同一段开头是:

  A return statement without an expression can be used only in functions 
  that do not return a value, that is, a function with the return type void,
  a constructor (12.1), or a destructor (12.4).

所以除了这些(非)圣战之外标准解释 有没有任何选项可以让 Qt 在编译时将其标记为错误?

Turns out that g++ compiler (used in Qt Creator by default) gives a mere warning if you don't have return statement in the non-void function, i.e.:

int* create_array(int n)
{
    int* a = new int[n];
}

compiles fine.

This behavior is subject to countless bug reports on g++ itself, but looks like developers consider this behavior conforming to C++ standard (which is debatable, cause it's a bit confusing in this part) as stated at http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43943 :

Flowing off the end of a function is equivalent to a return with no value; 
this results in undefined behavior in a value-returning function.

However, the very same paragraph begins with:

  A return statement without an expression can be used only in functions 
  that do not return a value, that is, a function with the return type void,
  a constructor (12.1), or a destructor (12.4).

So aside from these (un)holy wars over the standard interpretation are there any options to make Qt flag this as an error at compile time?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柳絮泡泡 2024-12-05 11:39:48

这与Qt无关,而是与g++有关。

在构建选项中,只需添加标志 -Werror ,g++ 就会将任何警告视为错误。您可能还需要使用标志 -Wall 来使 g++ 生成额外的警告,因为默认情况下它不会针对缺少 return 语句(以及许多其他情况)生成警告。然而,无法根据每个警告来调整此设置,因此要么全部执行,要么什么都不执行。

This is not related to Qt but to g++.

In the build options, simply add the flag -Werror and g++ will consider any warning as error. You may also need to use flag -Wall to make g++ generate additional warnings, as it doesn't generate warnings for missing return statements (and many other situations) by default. There's no way to tune this setting on a per-warning basis however, so it's either everything or nothing.

尽揽少女心 2024-12-05 11:39:48

您可以使用 -Werror=warning_name 语法将特定警告视为错误。对于您的情况,您可以使用 -Werror=return-type (其中 return-type 是“控件到达非 void 函数末尾”警告的名称)。根据 https://gcc.gnu.org/onlinedocs/gcc/Warning-选项.html

You can use -Werror=warning_name syntax to treat a specific warning as an error. For your case you can use -Werror=return-type (where return-type is the name of the "control reaches end of non-void function" warning). According to https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文