Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 941
Zebrafish Avatar asked Sep 17 '25 15:09

Zebrafish


2 Answers

<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.

like image 116
HolyBlackCat Avatar answered Sep 19 '25 06:09

HolyBlackCat


std::floor is defined in cmath.

Andoid NDK uses libc++, while the others were likely using MSVC or libstdc++. the standard doesn't restrict what headers will be transitively included, this depends on your standard library vendor, the standard only defines what you should include to guarantee having a specific function. and in case of std::floor it exists in cmath.

like image 43
Ahmed AEK Avatar answered Sep 19 '25 06:09

Ahmed AEK