Re: cmath vs. math.h vs isfinite

Valery Ushakov <[email protected]>
Newsgroups gmane.os.netbsd.devel.toolchain
Message-ID <[email protected]>
On Tue, Aug 25, 2026 at 07:16:29 -0400, Greg Troxel wrote:

> In C++, if you include <cmath>, and then want to use e.g. abs, the
> standards say that you should write std::abs().  But they allow
> compilers to make std::abs() available as just abs(), and some code does
> that.  I fairly frequently find instances of this, and submit upstream
> bug reports.  I have found that almost always, maybe always, upstreams
> fix this (or perhaps, do not say I'm wrong, don't remember if any
> nonresponsive).
> 
> I think what you are seeing is different, which is that things declared
> in C for math.h are omitted.
> 
> A further standards question: Why is it ok to include math.h in C++
> without extern "C"?

B/c it's "their" <math.h>, not ANSI C <math.h>.

  C compatibility headers

  For some of the C standard library headers of the form xxx.h, the C++
  standard library both includes an identically-named header and another
  header of the form cxxx (all meaningful cxxx headers are listed
  above).  The intended use of headers of form xxx.h is for
  interoperability only.  It is possible that C++ source files need to
  include one of these headers in order to be valid ISO C.  Source files
  that are not intended to also be valid ISO C should not use any of the
  C headers.

  With the exception of complex.h, each xxx.h header included in the C++
  standard library places in the global namespace each name that the
  corresponding cxxx header would have placed in the std namespace.

  These headers are allowed to also declare the same names in the std
  namespace, and the corresponding cxxx headers are allowed to also
  declare the same names in the global namespace: including <cstdlib>
  definitely provides std::malloc and may also provide ::malloc.
  Including <stdlib.h> definitely provides ::malloc and may also provide
  std::malloc.  This applies even to functions and function overloads
  that are not part of C standard library.

  Notes: xxx.h headers are deprecated in C++98 and undeprecated in
  C++23.  These headers are discouraged for pure C++ code, but not
  subject to future removal.

-uwe
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.