Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::expf and std::logf not recognized by gcc 7.2.0

Tags:

gcc

c++11

It seems that gcc (tried 7.2.0 and 5.4.0) does not have std::expf and std::logf - see coliru sample. Since cppreference says they were added in C++11 is there some gcc specific macro I am missing or are they in general missing in gcc?

like image 979
Rudolfs Bundulis Avatar asked Apr 01 '19 15:04

Rudolfs Bundulis


2 Answers

This is a bug in libstdc++, which ships with GCC. It does not fully conform to C++17 (as of v9.1.0 in June 2019). The copyright notice on the version of <cmath> that Ubuntu uses by default says it was last updated in 2016. Version 9.1.0 does have a #if __cplusplus > 201402L section, but it doesn’t declare the identifiers required by C++17. There is an open bug report.

It never declares expf or logf (nor cosf, sinf, etc.) within the std:: namespace, even though C++17 says it shall. The C++11 standard says, “Names that are defined as functions in C shall be defined as functions in the C++ standard library,” and “Each name from the Standard C library declared with external linkage is reserved to the implementation for use as a name with extern "C" linkage, both in namespace std and in the global namespace.” However, std::expf et al. are missing from the table of functions provided by <cmath> until P0175r1 in June 2016. This was apparently an oversight, but GCC has always made them available only in the global namespace.

The libc++ library does declare them, so compiling with clang++ -std=c++17 -stdlib=libc++ should work. You can also #include <math.h> to use them in the global namespace, or use the overloaded exp(), log(), etc. on float arguments.

like image 69
Davislor Avatar answered Oct 18 '22 16:10

Davislor


If you

#include <cmath>

you will get

float       exp ( float arg );
double      exp ( double arg );
long double exp ( long double arg );
double      exp ( IntegralType arg );

float       log ( float arg );
double      log ( double arg );
long double log ( long double arg );
double      log ( IntegralType arg );

And hence you can call just std::exp/std::log and let the compiler figure out the overload for you. If you want to call a mismatching overload (e.g. the float overload on a double variable), I find it much more explicit and clear to add a static_cast in those cases:

double bla = ...;
return std::exp(static_cast<float>(bla));

This is a strange construct any way you write it (e.g. why is bla not a float to begin with?), and hiding it in a single-letter function name suffix isn't helping anyone.

like image 43
rubenvb Avatar answered Oct 18 '22 14:10

rubenvb