以下程序编译正确:
#include <algorithm>
int main(int argc, char *argv[]) {
return int(log(23.f));
}
(在带有标志 -std=c++11
的 g++ 4.9.2 下)
代码使用函数log
,这是在 <cmath>
上定义的.但是,它不包括标题 <cmath>
, 只有标题 <algorithm>
.为什么是g++
没有给出任何警告,并正确编译代码?
最佳答案
根据标准,某些 header 确实包含其他 header 。例如,<cinttypes>
包括 <cstdint>
.请参阅包括部分here .关于 <algorithm>
, 没有关于它应该包括哪些其他标题的声明(参见 here )。所以,结论是,<algorithm>
不需要包含 <cmath>
,并且您的示例代码不可移植。它可能无法在其他 C++ 实现上编译。
关于c++ - <algorithm> 是否包含 <cmath>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29454488/