Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions stl/inc/__msvc_math.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <numbers> header is a Standard alternative.
#ifdef _USE_MATH_DEFINES
#include <corecrt_math_defines.h>
#endif

extern "C++" {
namespace _Msvc { // duplicate type traits not provided by <yvals_core.h>
template <class _Ty>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(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) ^^^