c ++ gcc错误:“ sqrtl”不是“ std”的成员

时间:2019-10-02 19:46:30

标签: c++ gcc std cmath

当我尝试编译以下程序时,编译器给出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的成员,否则编译器将给出与sqrtlsqrtf相同的错误。这尤其令人困惑,因为删除std::前面的cout会使编译器给我error: 'cout' was not declared in this scope

有人能解释为什么sqrtlsqrtsqrtf表现得如此奇怪吗? sqrt甚至是std的成员吗?我如何找出某个方法是否为std的成员?

我知道删除std::是一个简单的解决方法,但是出于一致性的目的,我希望在个人图书馆中的所有std::成员面前都使用std

1 个答案:

答案 0 :(得分:5)

这是一个错误。每个[cmath.syn] sqrtlstd命名空间的成员。

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);
      [...]
}

这是合法代码,将在MSVSClang中进行编译。

有一个bug report for this in GCC,但由于它很琐碎且资源有限,因此尚未得到照顾。