当我尝试编译以下程序时,编译器给出error: 'sqrtl' is not a member of 'std'
。
#include <cmath>
#include <iostream>
int main(){
std::cout << std::sqrtl(5.0) << "\n";
return 0;
}
我想知道为什么会这样,所以我开始尝试。
当我删除std::
前面的sqrtl
时,程序将编译并正常运行。当我另外删除#include <cmath>
时,编译器给出error: 'sqrtl' was not declared in this scope
。
在这一点上我真的很困惑。显然在sqrtl
中必须有一个函数cmath
,但是它不是std
的成员吗?
当我在原始程序中将sqrtl
替换为sqrt
时,该程序将编译并正常运行。当我删除std::
前面的sqrt
时也是一样。当我另外删除#include <cmath>
时,编译器给出error: 'sqrt' was not declared in this scope
。
最后,我对sqrtf
做了同样的测试。与sqrtl
发生的事情相同。
我觉得很奇怪的另一件事是,删除std::
可以使程序首先编译。特别是对于sqrt
,它必须是std
的成员,否则编译器将给出与sqrtl
和sqrtf
相同的错误。这尤其令人困惑,因为删除std::
前面的cout
会使编译器给我error: 'cout' was not declared in this scope
。
有人能解释为什么sqrtl
,sqrt
和sqrtf
表现得如此奇怪吗? sqrt
甚至是std
的成员吗?我如何找出某个方法是否为std
的成员?
我知道删除std::
是一个简单的解决方法,但是出于一致性的目的,我希望在个人图书馆中的所有std::
成员面前都使用std
。
答案 0 :(得分:5)
这是一个错误。每个[cmath.syn] sqrtl
是std
命名空间的成员。
namespace std { [...] float sqrt(float x); // see [library.c] double sqrt(double x); long double sqrt(long double x); // see [library.c] float sqrtf(float x); long double sqrtl(long double x); [...] }
有一个bug report for this in GCC,但由于它很琐碎且资源有限,因此尚未得到照顾。