`<__msvc_math.hpp>`: `_USE_MATH_DEFINES` is ignored after the first `<math.h>` include under `/Zc:cmath`
Maintainer thường phản hồi trong vòng 2 ngày
Đánh giá
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 72/100
Hướng nghiên cứu
Đọc stl/inc/__msvc_math.hpp trên main quanh dòng 6 và 29, sau đó so sánh kiểm tra _USE_MATH_DEFINES có bảo vệ của nó với khối không bảo vệ trong UCRT <math.h>. Tái hiện với meow.cpp được cung cấp bằng cl /Zc:cmath, sau đó xác định bài kiểm tra được thêm cho #6446 và mở rộng nó để bao phủ _USE_MATH_DEFINES được định nghĩa sau một lần include <cmath> hoặc <math.h> trước đó. Hoàn thành nghĩa là M_PI có sẵn trong trường hợp đó dưới /Zc:cmath, với bài kiểm tra mới chạy qua; phương án dự phòng là tài liệu hóa rằng macro phải được định nghĩa trước lần include đầu tiên.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
🤖 AI-generated (drafted with GitHub Copilot CLI; repros verified with the public 14.52.37025 toolset)
Describe the bug
Follow-up to #6446, which restored _USE_MATH_DEFINES support in <__msvc_math.hpp>. The restored check sits inside the include guard, and #6446's test only covers /D_USE_MATH_DEFINES on the command line, so the macro only works when defined before the first <math.h>.
With /Zc:cmath, #include <math.h> is redirected to <__msvc_math.hpp>. That header checks _USE_MATH_DEFINES inside its include guard (stl/inc/__msvc_math.hpp lines 6 and 29 on main), so the check only runs on the first inclusion.
The UCRT <math.h> this replaces has no include guard around that check:
#include <corecrt_math.h>
#ifdef _USE_MATH_DEFINES
#include <corecrt_math_defines.h>
#endif
Under /Zc:cmath, /showIncludes shows that the UCRT <math.h> is never opened, and a second #include <math.h> opens nothing. That contradicts the header's own comment at lines 9–13 ("the MSVC frontend arranges for this file to be included first … to neutralize the contents of math.h when it is processed after"), and it is why the UCRT's unguarded _USE_MATH_DEFINES block no longer helps.
So a translation unit that defines _USE_MATH_DEFINES and includes <math.h> after something else already included <math.h> or <cmath> gets M_PI without /Zc:cmath, and doesn't get it with /Zc:cmath. A precompiled header, or any common header that includes <cmath>, makes this the normal case in large codebases. In a large codebase we build, this broke about 35 call sites (M_PI undeclared) once /Zc:cmath was enabled.
The Microsoft Learn page for /Zc:cmath still says _USE_MATH_DEFINES "has no effect" in this mode. Since #6446 deliberately restored the support, that sentence is outdated (I can file a separate MicrosoftDocs/cpp-docs issue). Honoring the macro on every inclusion, as UCRT does, keeps existing code working.
Command-line test case
C:\Temp>type meow.cpp
#include <cmath>
#define _USE_MATH_DEFINES
#include <math.h>
int main() {
return M_PI > 3.0 ? 0 : 1;
}
C:\Temp>cl /EHsc /nologo /W4 /std:c++latest /c meow.cpp
meow.cpp
C:\Temp>cl /EHsc /nologo /W4 /std:c++latest /Zc:cmath /c meow.cpp
meow.cpp
meow.cpp(6): error C2065: 'M_PI': undeclared identifier
It fails the same way with /std:c++20 /Zc:cmath. Defining _USE_MATH_DEFINES before the first include works in both modes.
Expected behavior
M_PI is declared, as it is without /Zc:cmath. For example, move the block out of the guard:
#ifndef __MSVC_MATH_HPP
#define __MSVC_MATH_HPP
...
#endif // __MSVC_MATH_HPP
#ifdef _USE_MATH_DEFINES
#include <corecrt_math_defines.h> // has its own guard (_MATH_DEFINES_DEFINED)
#endif
Otherwise, document that _USE_MATH_DEFINES only works when defined before the first inclusion.
STL version
MSVC Build Tools 14.52.37025 (VS 2026 public, cl 19.52.37029.0). stl/inc/__msvc_math.hpp on main has the same structure.
Additional context
Including <corecrt_math_defines.h> directly, or using <numbers>, works in both modes. Both are tested workarounds.
- Ngôn ngữ chính
- C++
- Star
- 11.2k
- Fork
- 1.7k
- Merge trung bình
- 4 ngày 4 giờ
- Pull request đã merge (30 ngày)
- 14
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của microsoft/STL
-
LWG
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
Maintainer thường phản hồi trong vòng 2 ngày
-
generator LWG
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
Maintainer thường phản hồi trong vòng 2 ngày
-
LWG
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
Maintainer thường phản hồi trong vòng 2 ngày
-
enhancement
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
Maintainer thường phản hồi trong vòng 2 ngày
-
LWG
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 38/100
Maintainer thường phản hồi trong vòng 2 ngày
Tất cả issue của microsoft/STL
Issue tương tự
-
enhancement
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
ionut-mihalache/aqua#31 · 1 bình luận ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
Maintainer thường phản hồi trong vòng 1 ngày
-
needs-kind
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 63/100
villagesql/villagesql-server#1702 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 2 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
microsoft/vcpkg-tool#2191 · 1 reaction ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
harfbuzz/harfbuzz#6966 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày