当函数未显式返回默认返回路径上的值时强制发生错误?
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不知道确切的警告编号,但您可以使用 #pragma warning 强制将特定警告视为错误:
示例:
将警告 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:
will treat warning 4001 as error
如果您启用最大警告级别,并将警告视为错误,您一定会找到您正在寻找的内容。猜测否则会返回什么:函数返回类型的默认构造对象。
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.
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.