c++standard-library

Why is std::floor not found when including <math.h> in C++23 on Android?


this seems just like a little quirk that I can fix just by including <cmath> where std::floor is used, but still this is weird and I'm wondering if anyone knows what's happening. I have a project, I can compile it on MSVC, GCC and Clang in C++20 and C++23, no problems. In Android Studio I can compile with C++20, but if I set C++20 standard in my CMake configuration I get the error:

error: no member named 'floor' in namespace 'std'

The place where I need it I include <math.h>, which has worked fine for every other build configuration. I think math includes <cmath>. Anyway, I can make the error go away by ALSO including <cmath> where I need it, but is there a reason why this is happening in Android compiling with C++23? Like, I already have:

#include <math.h>

And with C++23 in Android Studio I literally have to include:

#include <cmath>

In order for std::floor to be recognised. The functions are commented out. Seems weird.


Solution

  • <cmath> declares things in namespace std. <math.h> declares things in the global namespace. (Same for all other C standard library headers.)

    Cppreference (and the standard) says that <cmath> is allowed to additionally declare things in the global namespace, and <math.h> is allowed additionally declare things in the std namespace.