c++ gcc error: 'sqrtl' is not a member of 'std'

c++ gcc error: 'sqrtl' is not a member of 'std'

当我尝试编译以下程序时,编译器给出 error: 'sqrtl' is not a member of 'std'.

#include <cmath>
#include <iostream>
int main(){
    std::cout << std::sqrtl(5.0) << "\n";
    return 0;
}

我想知道为什么会这样,所以我开始试验。

当我删除 sqrtl 前面的 std:: 时,程序编译并运行良好。当我另外删除 #include <cmath> 时,编译器给出 error: 'sqrtl' was not declared in this scope.

在这一点上我真的很困惑。在cmath中显然必须有一个函数sqrtl,但它不是std的成员?

当我将原程序中的sqrtl替换为sqrt时,程序编译并运行良好。当我删除 sqrt 前面的 std:: 时也是如此。当我另外删除 #include <cmath> 时,编译器给出 error: 'sqrt' was not declared in this scope.

最后,我用sqrtf做了同样的测试。同样的事情发生在 sqrtl.

我觉得奇怪的另一件事是删除 std:: 让程序首先编译。特别是 sqrt 必须是 std 的成员,否则编译器会给出与 sqrtlsqrtf 相同的错误。这特别令人困惑,因为在 cout 前面删除 std:: 会使编译器给我 error: 'cout' was not declared in this scope.

谁能解释为什么 sqrtlsqrtsqrtf 表现如此奇怪? sqrt 甚至是 std 的成员吗?我怎么知道某个方法是否是 std 的成员?

我知道删除 std:: 是一个简单的解决方法,但为了保持一致性,我希望在我的个人图书馆中的所有 std 成员面前显示 std::

这是一个错误。 Per [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);
      [...]
}

这是合法代码,将在 MSVS and Clang 中编译。

有一个bug report for this in GCC但是还没有处理,因为它很琐碎而且他们的资源是有限的。