Skip to content

<math.h>: Provide the abs() overloads under /Zc:cmath - #6535

Draft
Tiago (tiagomacarios) wants to merge 1 commit into
microsoft:mainfrom
tiagomacarios:fix-6533-math-h-abs
Draft

Tiago (tiagomacarios) wants to merge 1 commit into
microsoft:mainfrom
tiagomacarios:fix-6533-math-h-abs

Conversation

@tiagomacarios

Copy link
Copy Markdown
Member

Fixes #6533.

AI disclosure: the header and test changes and this description were written with GitHub Copilot, and checked as described under Testing.

Opening as a draft: this touches <__msvc_stdlib.hpp>, which is meant to stay close to the SDK's <stdlib.h>, so you may prefer a different shape (alternatives below), or to make the change yourselves.

Under /Zc:cmath, <math.h> declares no abs overloads: the integer ones are in <__msvc_stdlib.hpp> and the floating-point ones in <cstdlib>. [support.c.headers.other]/1 requires <math.h> to provide the abs overloads that <cmath> declares, and abs(int), labs and llabs worked with the UCRT's <math.h>.

This change:

  • moves abs(int), abs(long), abs(long long), labs and llabs from <__msvc_stdlib.hpp> into a new <__msvc_int_abs.hpp>, unchanged and in the same _Msvc_stdlib inline namespace, included at file scope by both <__msvc_stdlib.hpp> and <__msvc_math.hpp>;
  • defines the floating-point abs overloads in <__msvc_math.hpp> as the same global functions <cstdlib> defines, and has <cstdlib> skip its copies when <__msvc_math.hpp> is in use.

Alternatives: <__msvc_math.hpp> including <__msvc_stdlib.hpp> (one line, but brings all of <stdlib.h> into <math.h>); duplicating the integer definitions in both headers under a shared guard macro; or one <__msvc_abs.hpp> with all six overloads, which would also give <stdlib.h> the floating-point ones (it lacks them today in every mode).

The new header includes only UCRT headers, like <__msvc_stdlib.hpp>, so <stdlib.h> doesn't start including <yvals_core.h>: code that macroizes a keyword before <stdlib.h> keeps compiling.

Testing: new compile-only GH_006533_math_h_abs checks the six abs overloads, labs and llabs with <math.h> alone, with <stdlib.h> before or after, and with <cmath> after, at C++14 through latest, using NO_TEST_ENVIRONMENT_PREPARER so force_include.hpp's <stdlib.h> doesn't hide the problem. Locally with MSVC Build Tools 14.52.37025 (not the Preview toolset): it fails 15 of 24 configurations on main and passes all 24 with the change. P0533R9_constexpr_for_cmath_and_cstdlib, VSO_0000000_c_math_functions, GH_000519_cmath_overloads, GH_004609_heterogeneous_cmp_overloads, GH_001411_core_headers (these four also with /Zc:cmath added) and VSO_0157762_feature_test_macros pass. With /Zc:cmath, all header units build in topological order as in P1502R1, import std.compat; sees all six overloads, and #include <math.h> followed by import std; goes from C2668 to compiling. Not run: clang-cl, ASan, /clr, /BE, /analyze, the libc++ suite. clang-format and validate are clean.

Fixes microsoft#6533.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Copilot Review

Development

Successfully merging this pull request may close these issues.

<math.h> doesn't declare abs under /Zc:cmath

1 participant