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

オープン 初心者向け
#6,532 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 2 日以内に返信

@tiagomacarios がすでに取り組んでいます。

2026年11月21日 から。

  • #6534 @tiagomacarios による — オープン

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
72/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
cpp
領域
tooling

調査の方向性

stl/inc/__msvc_math.hpp を main 上の 6 行目と 29 行目付近で読み、次にそのガード付きの _USE_MATH_DEFINES チェックを UCRT <math.h> のガードなしブロックと比較してください。提供された meow.cpp で cl /Zc:cmath を使って再現し、次に #6446 に対して追加されたテストを見つけて、_USE_MATH_DEFINES が以前の <cmath> または <math.h> のインクルード後に定義される場合をカバーするように拡張してください。完了とは、その場合に M_PI が /Zc:cmath の下で利用可能であり、新しいテストが通ることを意味します。フォールバックは、マクロを最初のインクルード前に定義する必要があることを文書化することです。

索引モデルが issue の本文から書いたものです。

説明

🤖 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.

主要言語
C++
スター
11.2k
フォーク
1.7k
平均マージ
4日 4時間
マージ済み PR(30日)
14

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

microsoft/STL のほかの issue

microsoft/STL の issue をすべて見る

似ている issue

C++ の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。