From 038b164196a79014b8e741bfaad86e4914bbd07a Mon Sep 17 00:00:00 2001 From: "Stephan T. Lavavej" Date: Thu, 10 Sep 2026 12:17:00 -0700 Subject: [PATCH] Restore and test `_USE_MATH_DEFINES` support. --- stl/inc/__msvc_math.hpp | 7 +++++++ .../tests/P0533R9_constexpr_for_cmath_and_cstdlib/env.lst | 4 ++++ .../tests/P0533R9_constexpr_for_cmath_and_cstdlib/test.cpp | 6 ++++++ 3 files changed, 17 insertions(+) diff --git a/stl/inc/__msvc_math.hpp b/stl/inc/__msvc_math.hpp index 5934bd775f..457b20dab3 100644 --- a/stl/inc/__msvc_math.hpp +++ b/stl/inc/__msvc_math.hpp @@ -23,6 +23,13 @@ _STL_DISABLE_CLANG_WARNINGS #pragma push_macro("new") #undef new +// https://learn.microsoft.com/cpp/c-runtime-library/math-constants +// These non-Standard macros (`M_PI` etc.) are widely used, but at least they're opt-in. +// The header is a Standard alternative. +#ifdef _USE_MATH_DEFINES +#include +#endif + extern "C++" { namespace _Msvc { // duplicate type traits not provided by template diff --git a/tests/std/tests/P0533R9_constexpr_for_cmath_and_cstdlib/env.lst b/tests/std/tests/P0533R9_constexpr_for_cmath_and_cstdlib/env.lst index a25d22d1bc..24d60708e6 100644 --- a/tests/std/tests/P0533R9_constexpr_for_cmath_and_cstdlib/env.lst +++ b/tests/std/tests/P0533R9_constexpr_for_cmath_and_cstdlib/env.lst @@ -24,6 +24,8 @@ PM_CL="/EHsc /Zc:cmath /std:c++latest /MD" PM_CL="/EHsc /Zc:cmath /std:c++latest /MDd" PM_CL="/EHsc /Zc:cmath /std:c++latest /MT" PM_CL="/EHsc /Zc:cmath /std:c++latest /MTd" +# And test the non-Standard macros: +PM_CL="/EHsc /Zc:cmath /std:c++latest /MTd /D_USE_MATH_DEFINES" # Explicitly disable libc-based math: PM_CL="/EHsc /Zc:cmath- /std:c++14 /MD" @@ -46,3 +48,5 @@ PM_CL="/EHsc /Zc:cmath- /std:c++latest /MD" PM_CL="/EHsc /Zc:cmath- /std:c++latest /MDd" PM_CL="/EHsc /Zc:cmath- /std:c++latest /MT" PM_CL="/EHsc /Zc:cmath- /std:c++latest /MTd" +# And test the non-Standard macros: +PM_CL="/EHsc /Zc:cmath- /std:c++latest /MTd /D_USE_MATH_DEFINES" diff --git a/tests/std/tests/P0533R9_constexpr_for_cmath_and_cstdlib/test.cpp b/tests/std/tests/P0533R9_constexpr_for_cmath_and_cstdlib/test.cpp index b33dfc3d91..71b3c9f69b 100644 --- a/tests/std/tests/P0533R9_constexpr_for_cmath_and_cstdlib/test.cpp +++ b/tests/std/tests/P0533R9_constexpr_for_cmath_and_cstdlib/test.cpp @@ -690,3 +690,9 @@ int main() { #endif // ^^^ _HAS_CXX26 && defined(_MSVC_LIBC_MATH) ^^^ #endif // ^^^ defined(__cpp_lib_constexpr_cmath) ^^^ } + +#ifdef _USE_MATH_DEFINES +static_assert(static_cast(M_PI * 100.0) == 314, "Unexpected value for M_PI, reboot universe and try again."); +#elif defined(M_PI) +static_assert(false, "When _USE_MATH_DEFINES is not defined, M_PI should not be defined."); +#endif // ^^^ defined(M_PI) ^^^