[英]C++ code runs with missing header, why?
I just realized that I am supposed to include the #include<cstdlib>
required by abs()
for the abs()
function. 我只是意识到我应该包括
abs()
对于abs()
函数所需的#include<cstdlib>
。
#include<iostream>
using namespace std;
int main()
{
int result;
result = abs(-10);
cout << result << "\n";
return 0;
}
Why does this code still work, even though I forgot the important header ( #include<cstdlib>
)? 即使我忘记了重要的标头(
#include<cstdlib>
),为什么此代码仍然有效?
That's because iostream
indirectly includes definition for abs()
. 这是因为
iostream
间接包含abs()
定义。 It is allowed by the Standard, but should not be relied upon, because it's implementation-dependant (ie your code may not compile on some other compilers). 标准允许它,但不应依赖它,因为它取决于实现(即,您的代码可能无法在某些其他编译器上编译)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.