简体   繁体   English

为什么要在Ideone上编译?

[英]Why does this compile on Ideone?

Ok so I was messing around on Ideone and accidentally submitted this piece of code, however to my surprise it actually compiled and ran outputting a value of 0, here . 好的,所以我在Ideone乱搞并意外地提交了这段代码,但令我惊讶的是它实际编译并运行输出值为0, 这里

#include <iostream>

using namespace std;

const int five(  )
{
        const int i = 5;
}

int main() {
        cout << five(  ) << endl;
        return 0;
}

I then tried this in Visual Studio, and on Codepad however both failed to compile because five() does not return a value, as one would expect. 然后我在Visual Studio中尝试了这个,然而在Codepad上,两个都无法编译,因为five()没有返回值,正如人们所期望的那样。 My question, is of course, why does this compile fine on Ideone even though the code, to my understanding is wrong and shouldn't compile. 我的问题当然是,为什么这个在Ideone上编译正常,即使代码,我的理解是错误的,不应该编译。

Plain and simply (from C++11 6.6.3 "The return statement"): 简单而简单(来自C ++ 11 6.6.3“返回语句”):

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. 这会导致值返回函数中的未定义行为。

So the compiler is pretty much allowed to do whatever it wants. 所以编译器几乎可以做任何想做的事情。 Clearly, a diagnostic is something I'd prefer from a compiler, but there are times when it can be difficult to diagnose (like when the return is inside conditional logic, and the 'end' of the function would never be reached). 显然,诊断是我更喜欢编译器的东西,但有时很难诊断(比如返回是在条件逻辑中,并且永远不会达到函数的'结束')。

Note that I get the following warning with GCC 4.6.1 (using the Wall option): 请注意,我使用GCC 4.6.1(使用Wall选项)收到以下警告:

test.cpp:8:1: warning: no return statement in function returning non-void [-Wreturn-type]

I'm not sure what options ideone passes to GCC (I imagine that -Wall would do the same with the 4.3.4 version that ideone uses). 我不知道什么ideone传递给GCC的选项(我想象-Wall会做与ideone使用4.3.4版本相同)。

Some related information: 一些相关信息:

In C it's OK for a function that is declared to return a value to not actually do so in certain circumstances; 在C中,声明返回值的函数在某些情况下实际上不会这样做是可以的。 in C it only results in undefined behavior if the function's return value is actually used . 在C中,如果实际使用了函数的返回值,它只会导致未定义的行为。 Pre-standard C didn't always support the void type, so functions that didn't return anything were often declared to return int , explicitly or implicitly. 预标准C并不总是支持void类型,因此未返回任何内容的函数通常被声明为显式或隐式返回int From C99 6.9.1/12 "Function definitions": If the } that terminates a function is reached, and the value of the function call is used by the caller, the behavior is undefined. 从C99 6.9.1 / 12“函数定义”:如果到达终止函数的} ,并且调用者使用函数调用的值,则行为未定义。

Also, as mentioned in a couple comments, flowing off the end of main() is treated specially by C++ and C99 and later. 另外,正如一些注释中所提到的,流出main()的末尾是由C ++和C99以及后来专门处理的。

Not returning a value from a non-void function is a mistake, but not all compiler treats it as an error -- for example, GCC only emits a warning when it encounters this. 不从非void函数返回值是错误的,但并非所有编译器都将其视为错误 - 例如,GCC在遇到此错误时仅发出警告。 Other compilers may be paranoid (and they're right) and don't let you compile such code. 其他编译器可能是偏执的(并且它们是正确的)并且不允许您编译这样的代码。 Of course compiler behavior may be modified using different switches and options. 当然,可以使用不同的开关和选项修改编译器行为。

The return value of 0 is just a random value -- it might equally be 255, -1 or any other garbage, as doing this is undefined behavior (except for main, for which C99 specifies that an implicit 0 return value should be assumed). 返回值0只是一个随机值 - 它可能同样是255,-1或任何其他垃圾,因为这样做是未定义的行为(除了main,C99指定应该假定隐式0返回值) 。

It appears that ideone doesn't display warnings, it only displays the compiler output if there was an error. 似乎ideone不显示警告,它只在出现错误时显示编译器输出。 On the version of GCC that ideone is using (gcc 4.3) this isn't an error, it's just a warning. 在ideone正在使用的GCC版本(gcc 4.3)上,这不是错误,它只是一个警告。

The code has undefined behavior. 代码具有未定义的行为。 Even though what you're doing is wrong, the compiler is not required to diagnose it. 即使您正在做的事情是错误的,编译器也不需要诊断它。 The other point is that ideone uses what is now a fairly old version of gcc. 另一点是,ideone使用的是现在相当古老的gcc版本。 A reasonably current version of gcc (eg, 4.7) will at least give you a warning that your function is declared to return a value, but doesn't -- but not by default. 一个合理的当前版本的gcc(例如,4.7)将至少给你一个警告,声明你的函数被声明为返回一个值,但不是 - 但默认情况下不是。 You have to turn it on with something like -Wall to get the warning (but as a general rule, I'd always use at least -Wall with gcc). 你必须用类似-Wall东西来打开警告(但作为一般规则,我总是至少使用-Wall和gcc)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM