且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在不包含cmath库的情况下使用pow

更新时间:2022-03-28 22:49:58

在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.