当函数未显式返回默认返回路径上的值时强制发生错误?

发布于 2024-08-08 09:37:27 字数 114 浏览 1 评论 0原文

在 VC++ (VSTS 2008) 中,有没有一种方法可以对未在默认返回路径上显式返回值的函数(或任何其他快速方法来定位它们)强制编译器错误?

在同一问题上,对于这些函数实际返回什么有任何保证吗?

Is there a way, in VC++ (VSTS 2008), to froce a compiler error for functions that do not explicitly return a value on the default return path (Or any other quick way to locate them)?

On the same issue, is there any gaurentee as to what such functions actually return?

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

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

发布评论

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

评论(3

触ぅ动初心 2024-08-15 09:37:27

我不知道确切的警告编号,但您可以使用 #pragma warning 强制将特定警告视为错误:

示例:

#pragma warning( error: 4001)

将警告 4001 视为错误

I don't know exactly the warning number, but you can use #pragma warning for enforcing a specific warning to be treated as error:

Example:

#pragma warning( error: 4001)

will treat warning 4001 as error

踏雪无痕 2024-08-15 09:37:27

如果您启用最大警告级别,并将警告视为错误,您一定会找到您正在寻找的内容。猜测否则会返回什么:函数返回类型的默认构造对象。

If you enable max warning level, and treat warnings as errors, you'll surely find what you're looking for. A guess as to what will be returned otherwise: A default-constructed object of the function's return type.

乖乖兔^ω^ 2024-08-15 09:37:27

VC 会警告此问题的许多实例,但无法检测到某些实例。我多次发现它在函数模板中遗漏了这个问题,但我也在一些普通函数中看到了 int 。将警告视为错误(所有警告的编译器切换或特定警告的编译指示)将使得不可能忽略它发现的那些警告。

对于那些被 VC 忽视的地方,你必须使用更彻底的工具。据我所知,在 VSTS 中,您还可以为编译器抛出一个 /analyze 开关,让它发现更多问题。

类似 lint 的程序也有许多版本。

使用其他一些编译器也有帮助。第一次将 VS 项目移植到 GCC 可能相当困难,但我认为 Intel 的编译器可以用作 VC 的直接替代品,并立即编译 VC 项目。 Comeau C++ 也具有与 VC 完全兼容的开关,并且具有非常好的错误消息。

VC will warn about many instances of this problem, but fails to detect some. I've repeatedly caught it missing this problem in function templates, but I've seen int in some plain functions, too. Treating warnings as errors (compiler switch for all warnings or pragma for specifc ones) will make it impossible to overlook those it finds.

For those VC overlooks you have to use more thorough tools. AFAIK in VSTS you can also throw an /analyze switch for the compiler and have it find even more problems.

There's also many versions of lint-like programs.

Using some other compiler helps, too. Porting a VS project to GCC for the first time can be quite hard, but I think Intel's compiler can be used as a drop-in replacement for VC and compile VC projects right away. Comeau C++, too, has switches for being quite VC-compatible and has incredibly good errors messages.

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