[英]Forcing an error when a function doesn't explicitly return a value on the deafult return path?
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)? 在VC ++(VSTS 2008)中,是否有一种方法可以针对未在默认返回路径上显式返回值的函数(或其他用于查找它们的快速方法)引发编译器错误?
On the same issue, is there any gaurentee as to what such functions actually return? 在同一个问题上,这些职能实际上返回什么呢?
I don't know exactly the warning number, but you can use #pragma warning for enforcing a specific warning to be treated as error: 我不知道确切的警告编号,但是您可以使用#pragma warning强制将特定警告视为错误:
Example: 例:
#pragma warning( error: 4001)
will treat warning 4001 as error 将警告4001视为错误
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 will warn about many instances of this problem, but fails to detect some. VC会警告有关此问题的许多实例,但无法检测到某些情况。 I've repeatedly caught it missing this problem in function templates, but I've seen int in some plain functions, too.
我一再发现它在函数模板中遗漏了这个问题,但是我在一些普通函数中也看到过int。 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. 对于那些忽略VC的人,您必须使用更全面的工具。 AFAIK in VSTS you can also throw an
/analyze
switch for the compiler and have it find even more problems. 在VSTS中的AFAIK中,您还可以为编译器抛出一个
/analyze
开关,让它发现更多问题。
There's also many versions of lint-like programs. 还有很多类似lint的程序版本。
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.
第一次将VS项目移植到GCC可能非常困难,但是我认为Intel的编译器可以用作VC的直接替代品,并可以立即编译VC项目。 Comeau C++ , too, has switches for being quite VC-compatible and has incredibly good errors messages.
Comeau C ++也具有与VC完全兼容的开关,并且具有非常好的错误消息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.