在编程:使用C++的原理和实践(第六次印刷)的第3章中,Stroustrup声明(第68页):"注意sqrt()没有为int".
这是一个基于该章的简单C++程序:
#include "std_lib_facilities.h"
int main()
{
int n = 3;
cout << "Square root of n == " << sqrt(n) << "\n";
}
Run Code Online (Sandbox Code Playgroud)
鉴于上面的引用,我希望编译或运行该程序的过程在某种程度上失败.
令我惊讶的是,编译它(使用g ++(GCC)4.2.1)并运行它成功没有错误或警告,并产生以下非常好的输出:
Square root of n == 1.73205
Run Code Online (Sandbox Code Playgroud)
因此我的问题是:如果sqrt()真的没有为a定义int,为什么上面的程序不能以某种方式失败?