简体   繁体   English

当函数未在默认返回路径上显式返回值时,是否强制出错?

[英]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.

相关问题 函数不返回默认值 - Function doesn't return default value 复制分配功能不返回任何内容时,为什么编译器不引发错误? - Why doesn't compiler raise an error when copy assignment function doesn't return anything? 为什么 g++ 在返回 -1 时不返回错误 - why does g++ doesn't return error when return -1 返回int但未显式返回任何内容的函数的返回值是多少? 例如输出 - what is the return value of the function that returns int, but isn't returning anything explicitly. for instance output of 即使未明确构造函数调用,它也会破坏返回对象? C ++ - Function call destructs return object even when it isn't explicitly constructed? C++ 'return false'不会从函数返回 - 'return false' doesn't return from function 为什么将函数的返回值乘以零时不会短路? - Why doesn't it short-circuit when you multiply the return value of a function by zero? 没有警告该函数int f()不返回任何值? - No warnings for that function int f() doesn't return any value? 不返回任何值的函数的单元测试 - Unit testing of a function that doesn't return any value C++ 平凡 function:返回值类型与 function 返回类型不匹配:三元运算符 - C++ trivial function: return value type doesn't match the function return type: ternary operator
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM