如何在不包含cmath库的情况下使用pow [英] How is it possible to use pow without including cmath library
问题描述
我正在尝试学习C ++,并且正在使用MS Visual Studio2019.我有以下代码:
I'm trying to learn C++ and I'm using MS Visual Studio 2019. I have the code below:
#include <iostream>
int main()
{
std::cout << pow(10, 2);
}
如何在不包含cmath
的情况下进行编译和运行而不会出现错误?解决方案内部只有一个包含上述代码的文件.
How is it possible to compile and run without error without including cmath
? Inside the solution there is only one file with the above code.
推荐答案
在C ++中如何在不包含cmath库的情况下使用pow
How is possible in C++ to use pow without include cmath library
通过包含另一个包含<math.h>
标头的标头.
By including another header that includes the <math.h>
header.
不能保证标准库标头通常不会包含其他标头,也不能保证<iostream>
不会特别包含<cmath>
.也不能保证<iostream>
会包含该标头,因此在使用其他标准库实现或相同版本的其他版本时,该程序可能无法编译.
There is no guarantee that standard library headers won't include other headers in general, nor that <iostream>
won't include <cmath>
in particular. Nor is there a guarantee that <iostream>
will include that header, so this program may fail to compile when using another standard library implementation or another version of the same.
结论:永远不要依赖这种及物包括.始终直接包含您依赖其声明的所有标头,除非显式重复了传递包含(例如,保证<ios>
包含<iosfwd>
).您不能使用成功的编译作为证明您已经提供了所有必需的直接包含物.
In concluson: Never rely on such transitive inclusion. Always directly include all of the headers whose declarations you depend on, unless the transitive include is explicitly doumented (for example, <ios>
is guaranteed to include <iosfwd>
). You cannot use successful compilation as proof that you have provided all of the required direct inclusions.
这篇关于如何在不包含cmath库的情况下使用pow的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!