Skip to content

<__msvc_math.hpp>: Respect _USE_MATH_DEFINES on every inclusion - #6534

Open
Tiago (tiagomacarios) wants to merge 1 commit into
microsoft:mainfrom
tiagomacarios:fix-6532-late-use-math-defines
Open

Tiago (tiagomacarios) wants to merge 1 commit into
microsoft:mainfrom
tiagomacarios:fix-6532-late-use-math-defines

Conversation

@tiagomacarios

Copy link
Copy Markdown
Member

Fixes #6532.

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

<__msvc_math.hpp> checks _USE_MATH_DEFINES inside its include guard, so under /Zc:cmath the macro only works when it's defined before the first <math.h>. The UCRT's <math.h> checks it on every inclusion, so code that defines _USE_MATH_DEFINES and includes <math.h> after a precompiled header or <cmath> already did loses M_PI when it moves to /Zc:cmath. This moves the check after the include guard, keeping the _STL_COMPILER_PREPROCESSOR condition. Repeated #include <math.h> now re-opens the header, as the UCRT's does.

Testing: extended the existing _USE_MATH_DEFINES check in P0533R9_constexpr_for_cmath_and_cstdlib with the late-definition case. Locally with MSVC Build Tools 14.52.37025 (not the Preview toolset) and this branch's headers, the 20 /Zc:cmath configurations without /D_USE_MATH_DEFINES fail on main with C2065 'M_PI' and pass with the change; all 57 configurations I could run (everything except ASan, clang-cl, /clr, /BE, /analyze) compile, link and run. clang-format and validate are clean.

Fixes microsoft#6532.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@tiagomacarios
Tiago (tiagomacarios) requested a review from a team as a code owner November 21, 2026 05:29
Copilot AI balanced review requested due to automatic review settings November 21, 2026 05:29
@github-project-automation github-project-automation Bot moved this to Copilot Review in STL Code Reviews Nov 21, 2026
@azure-pipelines

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

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are reported for the change honoring late _USE_MATH_DEFINES opt-in on repeated inclusion.

No open issues 🎉

What changed in this PR

Fixes #6532 by making <__msvc_math.hpp> honor _USE_MATH_DEFINES on repeated inclusion under /Zc:cmath, matching UCRT behavior.

Changes:

  • Moves the math-constant inclusion outside the header guard while retaining the _STL_COMPILER_PREPROCESSOR condition.
  • Adds regression coverage for defining _USE_MATH_DEFINES after an earlier <math.h> inclusion.
File Description
tests/​std/​tests/​P0533R9_constexpr_for_cmath_and_cstdlib/​test.cpp Verifies that late opt-in exposes M_PI with the expected value.
stl/​inc/​__msvc_math.hpp Checks _USE_MATH_DEFINES on every inclusion and documents why.

Review effort: Balanced

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.

<__msvc_math.hpp>: _USE_MATH_DEFINES is ignored after the first <math.h> include under /Zc:cmath

2 participants