Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

std

gcc

cmath

When I try to compile the following program, the compiler gives error: 'sqrtl' is not a member of 'std'.

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

I wanted to know why this happens, so I started experimenting.

When I remove std:: in front of sqrtl the program compiles and runs fine. When I additionally remove #include <cmath>, the compiler gives error: 'sqrtl' was not declared in this scope.

At this point I am really confused. There clearly has to be a function sqrtl in cmath, but it is not a member of std?

When I replace sqrtl with sqrt in the original program, the program compiles and runs fine. Same thing when I remove std:: in front of sqrt. When I additionally remove #include <cmath>, the compiler gives error: 'sqrt' was not declared in this scope.

Finally, I did the same test with sqrtf. The same thing happened as with sqrtl.

Another thing I find weird is that removing std:: lets the program compile in the first place. Especially with sqrt which must be a member of std or the compiler would have given the same error as sqrtl and sqrtf. This is especially confusing since removing std:: in front of cout makes the compiler give me error: 'cout' was not declared in this scope.

Can anyone explain why sqrtl, sqrt and sqrtf behave so strangely? Is sqrt even a member of std? How could I find out whether a certain method is a member of std or not?

I know removing std:: is an easy fix, but for consistency purposes I like to have std:: in front of all std members in my personal library.

like image 820
SmileyCraft Avatar asked Oct 16 '25 02:10

SmileyCraft


1 Answers

This is a bug. Per [cmath.syn] sqrtl is a member of the std namespace.

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

This is legal code and will compile in MSVS and Clang.

There is a bug report for this in GCC but it has not yet been taken care of since it is very trivial and their resources are finite.

like image 168
NathanOliver Avatar answered Oct 18 '25 14:10

NathanOliver